开发环境
- 解释器版本:python 3.8
- 代码编辑器:pycharm 2021.2
股票涨幅缩略界面
导入模块
代码语言:javascript
复制import requests
import json
import csv
请求数据
代码语言:javascript
复制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 Object]'
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)
解析数据
代码语言:javascript
复制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)
保存数据
代码语言:javascript
复制with open('财经.csv', mode='a', newline='', encoding='utf-8') as f:
csv_writer = csv.writer(f)
csv_writer.writerow([name, price, updown, fiveminute])
运行代码,得到结果
股票涨幅详情页数据
请求数据
代码语言:javascript
复制url = 'http://quotes.money.163.com/hs/service/diyrank.php?host=http://quotes.money.163.com/hs/service/diyrank.php&page=1&query=STYPE:EQA&fields=NO,SYMBOL,NAME,PRICE,PERCENT,UPDOWN,FIVE_MINUTE,OPEN,YESTCLOSE,HIGH,LOW,VOLUME,TURNOVER,HS,LB,WB,ZF,PE,MCAP,TCAP,MFSUM,MFRATIO.MFRATIO2,MFRATIO.MFRATIO10,SNAME,CODE,ANNOUNMT,UVSNEWS&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)
解析数据
代码语言:javascript
复制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']
保存数据
代码语言:javascript
复制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])
运行代码,得到结果