开发环境
- 解释器版本:python 3.8
- 代码编辑器:pycharm 2021.2
股票涨幅缩略界面
导入模块
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案例讲解
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。