当前位置: 首页>移动开发>正文

python小案例——采集网易财经数据

鍓嶈█

澶у鏃╁ソ銆佸崍濂姐€佹櫄濂藉悥 鉂?~

python小案例——采集网易财经数据,第1张

寮€鍙戠幆澧?

  • 瑙i噴鍣ㄧ増鏈? python 3.8

  • 浠g爜缂栬緫鍣? pycharm 2021.2

  • requests: pip install requests

  • pandas: pip install pandas

  • pyecharts: pip install pyecharts

1. 浠€涔堟槸pachong?

閲囬泦浜掕仈缃?缃戦〉/app)鏁版嵁鐨勪竴椤规妧鏈?(鍥剧墖/闊抽/瑙嗛/鏂囨湰) + 鎶㈣喘鎿嶄綔 / 浜轰负鑳藉鍋氱殑涓€浜涙搷浣?鑴氭湰)

鍘熺悊:

妯℃嫙鎴?瀹㈡埛绔?缃戦〉/app) 鍚?鏈嶅姟鍣?鍙戦€佺綉缁滆姹?/p>

涓嶈璁╁鏂?鏈嶅姟鍣?闅惧彈 鐧惧害(鍚勫ぇ鎼滅储寮曟搸 閮芥槸鐖櫕)

2. 鏈夊摢浜涙楠?璇ュ浣曞疄鐜?

涓€. 鎬濊矾鍒嗘瀽

  1. 闇€瑕侀噰闆嗕粈涔堟暟鎹?/p>

    鑲$エ淇℃伅

  2. 鏁版嵁鍦ㄥ摢閲?/p>

    閫氳繃 f12 鎵撳紑 network(缃戠粶闈㈡澘) 璁板綍缃戦〉褰撲腑鐨勬暟鎹祫婧愮殑 鎶撳寘鍒嗘瀽

浜? 浠g爜瀹炵幇

  1. 鍙戦€佽姹? 閫氳繃浠g爜鐨勬柟寮忚闂綉绔?/p>

  2. 鑾峰彇鏁版嵁

  3. 瑙f瀽鏁版嵁 鎻愬彇鏁版嵁 鎶婃兂瑕佺殑鐣欎笅鏉?/p>

  4. 淇濆瓨鏁版嵁

浠g爜灞曠ず

瀵煎叆妯″潡

import requests     # 鍙戦€佽姹?
import csv
with open('璐㈢粡.csv', mode='a', newline='', encoding='utf-8') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(
        ['CODE', 'SNAME', 'PRICE', 'PERCENT', 'UPDOWN', 'FIVE_MINUTE', 'OPEN', 'YESTCLOSE', 'HIGH', 'LOW', 'VOLUME', 'TURNOVER', 'HS', 'LB', 'WB', 'ZF',
         'PE', 'MCAP', 'TCAP', 'MFSUM', 'MFRATIO2', 'MFRATIO10'])
for page in range(0, 213):
    # url 鏁版嵁鏉ユ簮 鏁版嵁鍖?缃戦〉閾炬帴
    url = f'http://      .money.163.com/hs/service/diyrank.php?host=http%3A%2F%2Fquotes.money.163.com%2Fhs%2Fservice%2Fdiyrank.php&page={page}&query=STYPE%3AEQA&fields=NO%2CSYMBOL%2CNAME%2CPRICE%2CPERCENT%2CUPDOWN%2CFIVE_MINUTE%2COPEN%2CYESTCLOSE%2CHIGH%2CLOW%2CVOLUME%2CTURNOVER%2CHS%2CLB%2CWB%2CZF%2CPE%2CMCAP%2CTCAP%2CMFSUM%2CMFRATIO.MFRATIO2%2CMFRATIO.MFRATIO10%2CSNAME%2CCODE%2CANNOUNMT%2CUVSNEWS&sort=PERCENT&order=desc&count=24&type=query'

浠g爜閲岀綉鍧€灏辩粰澶у灞忚斀浜嗭紝涓嶇劧鍙戜笉鍑烘潵

瀹屾暣浠g爜鏈夐渶瑕佺殑鍙互鏂囩珷涓嬫柟鍚嶇墖鑾峰彇鍝

  1. 鍙戦€佽姹?/p>

    .get / .post 鐖櫕99%璇锋眰鏂瑰紡閮芥槸杩欎袱绉?/p>

    response = requests.get(url=url)
  1. 鑾峰彇鏁版嵁

    .text: 鑾峰彇鏂囨湰鍐呭

    .content: 鑾峰彇浜岃繘鍒舵暟鎹?(鍥剧墖/闊抽/瑙嗛)

    .json(): {}鎵€鍖呰9鐨勬暟鎹?.json()鍙栧€?瑙f瀽鏁版嵁灏变細鏇存柟渚?鏇寸畝鍗?瀛楀吀

    json_data = response.json()
  1. 瑙f瀽鏁版嵁
    list = json_data['list']
    for i in range(0, len(list)):
        CODE = list[i]['CODE']
        SNAME = list[i]['SNAME']
        PRICE = list[i]['PRICE']
        PERCENT = list[i]['PERCENT']
        UPDOWN = list[i]['UPDOWN']
        FIVE_MINUTE = list[i]['FIVE_MINUTE']
        OPEN = list[i]['OPEN']
        YESTCLOSE = list[i]['YESTCLOSE']
        HIGH = list[i]['HIGH']
        LOW = list[i]['LOW']
        VOLUME = list[i]['VOLUME']
        TURNOVER = list[i]['TURNOVER']
        HS = list[i]['HS']
        try:
            LB = list[i]['LB']
        except:
            LB = ''
        WB = list[i]['WB']
        ZF = list[i]['ZF']
        try:
            PE = list[i]['PE']
        except:
            PE = ''
        MCAP = list[i]['MCAP']
        TCAP = list[i]['TCAP']
        MFSUM = list[i]['MFSUM']
        MFRATIO2 = list[i]['MFRATIO']['MFRATIO2']
        MFRATIO10 = list[i]['MFRATIO']['MFRATIO10']
        print(CODE, SNAME, PRICE, PERCENT, UPDOWN, FIVE_MINUTE, OPEN, YESTCLOSE, HIGH, LOW, VOLUME, TURNOVER, HS, LB, WB, ZF, PE, MCAP, TCAP, MFSUM, MFRATIO2, MFRATIO10)
  1. 淇濆瓨鏁版嵁
        with open('璐㈢粡.csv', mode='a', newline='', encoding='utf-8') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow([CODE, SNAME, PRICE, PERCENT, UPDOWN, FIVE_MINUTE, OPEN, YESTCLOSE, HIGH, LOW, VOLUME, TURNOVER, HS, LB, WB, ZF, PE, MCAP, TCAP, MFSUM, MFRATIO2, MFRATIO10])

灏捐 馃挐

瑕佹垚鍔燂紝鍏堝彂鐤紝涓嬪畾鍐冲績寰€鍓嶅啿锛?/p>

瀛︿範鏄渶瑕侀暱鏈熷潥鎸佺殑锛屼竴姝ヤ竴涓剼鍗板湴璧板悜鏈潵锛?/p>

鏈潵鐨勪綘涓€瀹氫細鎰熻阿浠婂ぉ瀛︿範鐨勪綘銆?/p>

鈥斺€?蹇冪伒楦℃堡

鏈枃绔犲埌杩欓噷灏辩粨鏉熷暒~鎰熷叴瓒g殑灏忎紮浼村彲浠ュ鍒朵唬鐮佸幓璇曡瘯鍝?馃槤

python小案例——采集网易财经数据,第2张

https://www.xamrdz.com/mobile/4p21994482.html

相关文章: