前言
截止2019年年底我国股票投资者数量为15975.24万户, 如此多的股民热衷于炒股,首先抛开炒股技术不说, 那么多股票数据是不是非常难找, 找到之后是不是看着密密麻麻的数据是不是头都大了?
今天带大家爬取雪球平台的股票数据, 并且实现数据可视化
先看下效果图
基本环境配置
- python 3.6
- pycharm
- requests
- csv
- time
目标地址
代码语言:javascript复制https://xueqiu.com/hq
爬虫代码
请求网页
代码语言:javascript复制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()
#想要完整代码的同学请关注微信公众号:松鼠爱吃饼干
#发送信息‘股票’即可获取
解析数据
代码语言:javascript复制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)
保存数据
代码语言:javascript复制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()
数据分析代码
代码语言:javascript复制c = (
Bar()
.add_xaxis(list(df2['股票名称'].values))
.add_yaxis("股票成交量情况", list(df2['成交量'].values))
.set_global_opts(
title_opts=opts.TitleOpts(title="成交量图表 - Volume chart"),
datazoom_opts=opts.DataZoomOpts(),
)
.render("data.html")
)