前言今天給大家分享的是如何通過(guò)Python爬蟲采集網(wǎng)易財(cái)經(jīng)易股票交易數(shù)據(jù),。 開(kāi)發(fā)工具Python版本: 3.8 相關(guān)模塊: requests模塊 環(huán)境搭建安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可,。 思路分析本文以爬蟲網(wǎng)易財(cái)經(jīng)數(shù)據(jù),,講解如何采集股票歷史交易數(shù)據(jù) 要爬取股票數(shù)據(jù)如下圖所示: 股票數(shù)據(jù) 提取頁(yè)面數(shù)據(jù)瀏覽器中打開(kāi)我們要爬取的頁(yè)面 按F12進(jìn)入開(kāi)發(fā)者工具,查看我們想要的數(shù)據(jù)在哪里 這里我們需要股票頁(yè)面數(shù)據(jù)就可以了 頁(yè)面數(shù)據(jù) 代碼實(shí)現(xiàn)# 1. 發(fā)送請(qǐng)求
response = requests.get(url)
# 2. 獲取數(shù)據(jù)
json_data = response.json()
# 3. 解析數(shù)據(jù)
list = json_data['list'] # 所有的股票信息
for i in list:
# i 代表的是每一個(gè)股票的 信息
SYMBOL = i['SYMBOL']
NAME = i['NAME']
PRICE = i['PRICE']
PERCENT = i['PERCENT']
UPDOWN = i['UPDOWN']
FIVE_MINUTE = i['FIVE_MINUTE']
OPEN = i['OPEN']
YESTCLOSE = i['YESTCLOSE']
HIGH = i['HIGH']
LOW = i['LOW']
VOLUME = i['VOLUME']
TURNOVER = i['TURNOVER']
HS = i['HS']
try:
LB = i['LB']
except:
LB = ""
WB = i['WB']
ZF = i['ZF']
try:
PE = i['PE']
except:
PE = ""
TCAP = i['TCAP']
MFSUM = i['MFSUM']
MFRATIO2 = i['MFRATIO']['MFRATIO2']
MFRATIO10 = i['MFRATIO']['MFRATIO10']
print(SYMBOL, NAME, PRICE, PERCENT, UPDOWN, FIVE_MINUTE, OPEN, YESTCLOSE, HIGH, LOW, VOLUME, TURNOVER, HS, LB, WB, ZF, PE, TCAP, MFSUM, MFRATIO2, MFRATIO10)
# 4. 保存數(shù)據(jù)
csv_writer.writerow([SYMBOL, NAME, PRICE, PERCENT, UPDOWN, FIVE_MINUTE, OPEN, YESTCLOSE, HIGH, LOW, VOLUME, TURNOVER, HS, LB, WB, ZF, PE, TCAP, MFSUM, MFRATIO2, MFRATIO10])
結(jié)果展示結(jié)果 ps:圖片僅供參考 最后為了感謝讀者們,,我想把我最近收藏的一些編程干貨分享給大家,,回饋每一個(gè)讀者,希望能幫到你們,。 里面有適合小白新手的全套資料給到大家~ 快來(lái)和小編一起成長(zhǎng)進(jìn)步吧,! ① 100+多本Python電子書(主流和經(jīng)典的書籍應(yīng)該都有了) ② Python標(biāo)準(zhǔn)庫(kù)資料(最全中文版) ③ 爬蟲項(xiàng)目源碼(四五十個(gè)有趣且經(jīng)典的練手項(xiàng)目及源碼) ④ Python基礎(chǔ)入門、爬蟲,、web開(kāi)發(fā),、大數(shù)據(jù)分析方面的視頻(適合小白學(xué)習(xí)) ⑤ Python學(xué)習(xí)路線圖(告別不入流的學(xué)習(xí))
|