python爬取关注度高股票,Python爬取股票数据,让你感受一下什么是一秒钟两千条数据...
本文的文字及圖片過濾網(wǎng)絡(luò),可以學(xué)習(xí),交流使用,不具有任何商業(yè)用途,如有問題請及時聯(lián)系我們以作處理。
以下文章來源于青燈編程,作者:清風(fēng)
Python GUI編程:高清電影在線觀看平臺制作,全網(wǎng)電影免費看
https://www.bilibili.com/video/BV1tz4y1o7Yc/
讓你感受一下什么是一秒鐘下載兩千條數(shù)據(jù)。。
基本開發(fā)環(huán)境
Python 3.6
皮查姆
相關(guān)模塊的使用
import csv
import time
import requests
import concurrent.futures
目標網(wǎng)頁分析
一共214頁的數(shù)據(jù),每頁數(shù)據(jù)20條,總計是4280。
:開發(fā)者工具,點擊第二頁,在XHR里面會出現(xiàn)數(shù)據(jù)。。
這是鏈接的參數(shù),其中的pn對應(yīng)就是頁碼,選擇的第二頁所以pn:2
如果細心的話,可以發(fā)現(xiàn)返回的數(shù)據(jù)并非是一個json數(shù)據(jù)。
這樣的數(shù)據(jù)提取肯定是轉(zhuǎn)換成json數(shù)據(jù)才好提取。這有兩個方法:
方法一:
把參數(shù)中的< cb:jQuery1124036392017581464287_1608882113715 >去掉,不打算進去,就可以直接以response.json()的形式輸出。
import requests
url = 'http://49.push2.eastmoney.com/api/qt/clist/get'
params = {
# 'cb': 'jQuery1124036392017581464287_1608882113715',
'pn': '2',
'pz': '20',
'po': '1',
'np': '1',
'ut': 'bd1d9ddb04089700cf9c27f6f7426281',
'fltt': '2',
'invt': '2',
'fid': 'f3',
'fs': 'm:0 t:6,m:0 t:13,m:0 t:80,m:1 t:2,m:1 t:23',
'fields': 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152',
'_': '1608882115527',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, params=params, headers=headers)
html_data = response.json()
stock_data = html_data['data']['diff']
方法二:
1,正常參數(shù),請求網(wǎng)頁返回數(shù)據(jù)response.txt
2,用正則匹配 jQuery1124036392017581464287_1608882113715(。?) 匹配中間的數(shù)據(jù)*
3,通過導(dǎo)入json模塊,串聯(lián)轉(zhuǎn)json數(shù)據(jù)json.loads
import pprint
import re
import requests
import json
url = 'http://49.push2.eastmoney.com/api/qt/clist/get'
params = {
'cb': 'jQuery1124036392017581464287_1608882113715',
'pn': '2',
'pz': '20',
'po': '1',
'np': '1',
'ut': 'bd1d9ddb04089700cf9c27f6f7426281',
'fltt': '2',
'invt': '2',
'fid': 'f3',
'fs': 'm:0 t:6,m:0 t:13,m:0 t:80,m:1 t:2,m:1 t:23',
'fields': 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152',
'_': '1608882115527',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, params=params, headers=headers)
result = re.findall('jQuery1124036392017581464287_1608882113715\((.*?)\);', response.text)[0]
html_data = json.loads(result)
stock_data = html_data['data']['diff']
pprint.pprint(stock_data)
對于這個網(wǎng)站,以上兩種方法都是可以的,但是一般建議使用第二種方式,因為第一種方式畢竟是投機取巧。
取值之后是一個列表的數(shù)據(jù),通過用于循環(huán)遍歷即可,獲取每一支股票的相關(guān)數(shù)據(jù)了,通過鍵值對取值獲取相對應(yīng)的數(shù)據(jù)即可。
for i in stock_data:
dit = {
'代碼': i['f12'],
'名稱': i['f14'],
'最新價': i['f2'],
'漲跌幅': str(i['f3']) + '%',
'漲跌額': i['f4'],
'成交量(手)': i['f5'],
'成交額': i['f6'],
'振幅': str(i['f7']) + '%',
'最高': i['f15'],
'最低': i['f16'],
'今開': i['f17'],
'昨收': i['f18'],
'量比': i['f10'],
'換手率': str(i['f8']) + '%',
'市盈率(動態(tài))': i['f9'],
'市凈率': i['f23'],
}
保存數(shù)據(jù)通過csv模塊保存就可以了。
使用多線程爬取速度有多快?
給五個線程時的速度:
if __name__ == '__main__':
start_time = time.time()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
for page in range(1, 215):
url = f'http://49.push2.eastmoney.com/api/qt/clist/get?pn={page}&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:13,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1608882114076'
executor.submit(main, url)
executor.shutdown()
總耗時:3.685572624206543
總計數(shù)據(jù):4279條數(shù)據(jù)
所以平均每秒鐘爬取1161條數(shù)據(jù)。
當(dāng)我給10個線程時的速度:
if __name__ == '__main__':
start_time = time.time()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=10)
for page in range(1, 215):
url = f'http://49.push2.eastmoney.com/api/qt/clist/get?pn={page}&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:13,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1608882114076'
executor.submit(main, url)
executor.shutdown()
總耗時:1.7553794384002686
總計數(shù)據(jù):4279條數(shù)據(jù)
所以平均每秒鐘爬取2437條數(shù)據(jù)。
當(dāng)我給20個線程時的速度:
。
。
。
。
。
。
。
。
。
。
給不了,電腦頂不住。
總結(jié)
以上是生活随笔為你收集整理的python爬取关注度高股票,Python爬取股票数据,让你感受一下什么是一秒钟两千条数据...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站制作教程,一眼看懂网站怎么建
- 下一篇: 一文教你学会python读取文本及字符串