前言
截止2019年年底我國股票投資者數量為15975.24萬戶, 如此多的股民熱衷於炒股,首先拋開炒股技術不說, 那麼多股票資料是不是非常難找, 找到之後是不是看著密密麻麻的資料是不是頭都大了?
今天帶大家爬取雪球平臺的股票資料, 並且實現資料視覺化
先看下效果圖
基本環境配置
- python 3.6
- pycharm
- requests
- csv
- time
目標地址
https://xueqiu.com/hq
爬蟲程式碼
請求網頁
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()
資料分析程式碼
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") )