基于Python爬取股票数据过程详解
(编辑:jimmy 日期: 2024/11/17 浏览:3 次 )
基本环境配置
- python 3.6
- pycharm
- requests
- csv
- time
相关模块pip安装即可
目标网页
分析网页
一切的一切都在图里
找到数据了,直接请求网页,解析数据,保存数据
请求网页
import requests url = 'https://xueqiu.com/service/v5/stock/screener/quote/list' response = requests.get(url=url, params=params, headers=headers, cookies=cookies) html_data = response.json()
解析数据
data_list = html_data['data']['list'] for i in data_list: dit = {} dit['股票代码'] = i['symbol'] dit['股票名字'] = i['name'] dit['当前价'] = i['current'] dit['涨跌额'] = i['chg'] dit['涨跌幅/%'] = i['percent'] dit['年初至今/%'] = i['current_year_percent'] dit['成交量'] = i['volume'] dit['成交额'] = i['amount'] dit['换手率/%'] = i['turnover_rate'] dit['市盈率TTM'] = i['pe_ttm'] dit['股息率/%'] = i['dividend_yield'] dit['市值'] = i['market_capital'] print(dit)
保存数据
import csv f = open('股票数据.csv', mode='a', encoding='utf-8-sig', newline='') csv_writer = csv.DictWriter(f, fieldnames=['股票代码', '股票名字', '当前价', '涨跌额', '涨跌幅/%', '年初至今/%', '成交量', '成交额', '换手率/%', '市盈率TTM', '股息率/%', '市值']) csv_writer.writeheader() csv_writer.writerow(dit) f.close()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇:Python类的继承super相关原理解析