亲宝软件园·资讯

展开

基于Python爬取股票数据过程详解

六月流火 人气:0

基本环境配置

  • 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()

加载全部内容

相关教程
猜你喜欢
用户评论