基於Python爬取股票資料過程詳解
阿新 • • 發佈:2020-10-23
基本環境配置
- 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()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。