编程语言
首页 > 编程语言> > Python采集网易财经股票行情排行榜数据

Python采集网易财经股票行情排行榜数据

作者:互联网

开发环境

股票涨幅缩略界面

导入模块

import requests
import json
import csv

请求数据

url = 'http://api.money.126.net/data/feed/RANK_SC_PLATE_HANGYE_ALL,RANK_SC_PLATE_HANGYE_UP,RANK_SC_PLATE_HANGYE_DOWN,RANK_SC_PLATE_GAINIAN_ALL,0000001,1399001,1399300,HSRANK_COUNT_SHA,HSRANK_COUNT_SZA,HSRANK_COUNT_SH3,RANK_SC_PLATE_GAINIAN_UP,RANK_SC_PLATE_GAINIAN_DOWN,RANK_SC_PLATE_DIYU_UP,RANK_SC_PLATE_DIYU_DOWN,RANK_AUP,RANK_A5MINUP,RANK_ADOWN,RANK_A5MINDOWN,RANK_ATURNOVERUP,RANK_AHSUP,RANK_AVOLUMECHGUP,RANK_AZFUP,RANK_KCBUP,RANK_KCB5MINUP,RANK_KCBDOWN,RANK_KCB5MINDOWN,RANK_KCBCJL,RANK_KCBHS,RANK_KCBLB,RANK_KCBZF,RANK_ZXBUP,RANK_ZXB5MINUP,RANK_ZXBDOWN,RANK_ZXB5MINDOWN,RANK_ZXBCJL,RANK_ZXBHS,RANK_ZXBLB,RANK_ZXBZF,RANK_CYBUP,RANK_CYB5MINUP,RANK_CYBDOWN,RANK_CYB5MINDOWN,RANK_CYBCJL,RANK_CYBHS,RANK_CYBLB,RANK_CYBZF,RANK_BUP,RANK_B5MINUP,RANK_BDOWN,RANK_B5MINDOWN,RANK_BTURNOVERUP,RANK_BHSUP,RANK_BVOLUMECHGUP,RANK_BZFUP,TING_PAI_TI_SHI_RANK,XIN_GU_SHANG_SHI_RANK,XIAN_SHOU_JIE_JIN_RANK,FEN_HONG_SONG_GU_RANK,RANK_SC_GONG_GAO?callback=ne_1655882778632&[object%20Object]'
headers = {
    'Host': 'api.money.126.net',
    'Referer': 'http://quotes.money.163.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
}
response = requests.get(url=url, headers=headers)

解析数据

json_str = response.text
json_str = json_str[17:-2]
json_dict = json.loads(json_str)
AHSUP_list = json_dict['RANK_A5MINUP']['list']
for ahsup in AHSUP_list:
    name = ahsup['name']
    price = ahsup['price']
    updown = ahsup['updown']
    fiveminute = ahsup['fiveminute']
    print(name, price, updown, fiveminute)

保存数据

with open('财经.csv', mode='a', newline='', encoding='utf-8') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow([name, price, updown, fiveminute])

运行代码,得到结果


股票涨幅详情页数据

请求数据

url = f'http://quotes.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=FIVE_MINUTE&order=desc&count=24&type=query'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
}
response = requests.get(url=url, headers=headers)

解析数据

json_data = response.json()
list_ = json_data['list']
for i in list_:
    CODE = i['CODE']
    SNAME = i['SNAME']
    PRICE = i['PRICE']
    PERCENT = i['PERCENT']
    UPDOWN = i['UPDOWN']
    FIVE_MINUTE = i['FIVE_MINUTE']
    OPEN = i['OPEN']
    YESTCLOSE = i['YESTCLOSE']
    HIGH = i['HIGH']
    LOW = i['LOW']
    VOLUME = i['VOLUME']
    TURNOVER = i['TURNOVER']
    HS = i['HS']
    LB = i['LB']
    WB = i['WB']
    ZF = i['ZF']
    PE = i['PE']
    MCAP = i['MCAP']
    TCAP = i['TCAP']
    MFSUM = i['MFSUM']
    MFRATIO2 = i['MFRATIO']['MFRATIO2']
    MFRATIO10 = i['MFRATIO']['MFRATIO10']

保存数据

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])

运行代码,得到结果

 

视频号个人主页:松鼠爱吃饼干
200+ Python案例讲解

 

 

标签:PLATE,网易,股票行情,Python,list,RANK,json,SC,csv
来源: https://www.cnblogs.com/qshhl/p/16494037.html