python爬蟲(chóng)中抓取指數(shù)的實(shí)例講解
有一些數(shù)據(jù)我們是沒(méi)法直觀的查看的,需要通過(guò)抓取去獲得。聽(tīng)到指數(shù)這個(gè)詞,有的小伙伴們覺(jué)得很復(fù)雜,似乎只在股票的時(shí)候才聽(tīng)說(shuō)的,比如一些數(shù)據(jù)的漲跌分析都是比較棘手的問(wèn)題。不過(guò)指數(shù)對(duì)于我們的數(shù)據(jù)分析還是很有幫助的,今天小編就python爬蟲(chóng)中抓取指數(shù)得方法給大家?guī)?lái)講解。
剛好這幾天需要用到這個(gè)爬蟲(chóng),結(jié)果發(fā)現(xiàn)baidu指數(shù)的請(qǐng)求有點(diǎn)變化,所以就改了改:
import requests
import sys
import time
word_url = 'http://index.baidu.com/api/SearchApi/thumbnail?area=0&word={}'
COOKIES = ''
headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'no-cache',
'Cookie': COOKIES,
'DNT': '1',
'Host': 'index.baidu.com',
'Pragma': 'no-cache',
'Proxy-Connection': 'keep-alive',
'Referer': 'http://index.baidu.com/v2/main/index.html',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
def decrypt(t,e):
n = list(t)
i = list(e)
a = {}
result = []
ln = int(len(n)/2)
start = n[ln:]
end = n[:ln]
for j,k in zip(start, end):
a.update({k: j})
for j in e:
result.append(a.get(j))
return ''.join(result)
def get_ptbk(uniqid):
url = 'http://index.baidu.com/Interface/ptbk?uniqid={}'
resp = requests.get(url.format(uniqid), headers=headers)
if resp.status_code != 200:
print('獲取uniqid失敗')
sys.exit(1)
return resp.json().get('data')
def get_index_data(keyword, start='2011-01-03', end='2019-08-05'):
keyword = str(keyword).replace("'", '"')
url = f'http://index.baidu.com/api/SearchApi/index?area=0&word={keyword}&area=0&startDate={start}&endDate={end}'
resp = requests.get(url, headers=headers)
print('獲取指數(shù)失敗')
content = resp.json()
data = content.get('data')
user_indexes = data.get('userIndexes')[0]
uniqid = data.get('uniqid')
ptbk = get_ptbk(uniqid)
while ptbk is None or ptbk == '':
ptbk = get_ptbk(uniqid)
all_data = user_indexes.get('all').get('data')
result = decrypt(ptbk, all_data)
result = result.split(',')
print(result)
if __name__ == '__main__':
words = [[{"name": "酷安", "wordType": 1}]]
get_index_data(words)
輸出:
運(yùn)行代碼就可以得到我們想要的指數(shù)了,當(dāng)然也可以用來(lái)看股票以及其他的一些操作,運(yùn)用python爬蟲(chóng)解決都是不錯(cuò)的選擇,感興趣的小伙伴也可以跟著小編嘗試一下。
到此這篇關(guān)于python爬蟲(chóng)中抓取指數(shù)的實(shí)例講解的文章就介紹到這了,更多相關(guān)python爬蟲(chóng)中如何抓取指數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python實(shí)現(xiàn)的爬取百度文庫(kù)功能示例
- 用python下載百度文庫(kù)的代碼
- python爬蟲(chóng)爬取淘寶商品比價(jià)(附淘寶反爬蟲(chóng)機(jī)制解決小辦法)
- python爬蟲(chóng)多次請(qǐng)求超時(shí)的幾種重試方法(6種)
- python爬蟲(chóng)搭配起B(yǎng)ilibili唧唧的流程分析
- 用sleep間隔進(jìn)行python反爬蟲(chóng)的實(shí)例講解
- celery在python爬蟲(chóng)中定時(shí)操作實(shí)例講解
- Python爬蟲(chóng)框架Scrapy安裝使用步驟
- 使用Python編寫(xiě)簡(jiǎn)單網(wǎng)絡(luò)爬蟲(chóng)抓取視頻下載資源
- 零基礎(chǔ)寫(xiě)python爬蟲(chóng)之使用Scrapy框架編寫(xiě)爬蟲(chóng)
- 零基礎(chǔ)寫(xiě)python爬蟲(chóng)之使用urllib2組件抓取網(wǎng)頁(yè)內(nèi)容
- python 爬取百度文庫(kù)并下載(免費(fèi)文章限定)
相關(guān)文章
Python通過(guò)matplotlib繪制動(dòng)畫(huà)簡(jiǎn)單實(shí)例
這篇文章主要介紹了Python通過(guò)matplotlib繪制動(dòng)畫(huà)簡(jiǎn)單實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
python對(duì)RabbitMQ的簡(jiǎn)單入門(mén)使用教程
RabbitMq是實(shí)現(xiàn)了高級(jí)消息隊(duì)列協(xié)議(AMQP)的開(kāi)源消息代理中間件,下面這篇文章主要給大家介紹了關(guān)于python對(duì)RabbitMQ的簡(jiǎn)單入門(mén)使用,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Pytorch實(shí)現(xiàn)ResNet網(wǎng)絡(luò)之Residual Block殘差塊
這篇文章主要為大家介紹了Pytorch實(shí)現(xiàn)ResNet網(wǎng)絡(luò)之Residual Block殘差塊實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Python基于yaml文件配置logging日志過(guò)程解析
這篇文章主要介紹了Python基于yaml文件配置logging日志過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Python內(nèi)置的HTTP協(xié)議服務(wù)器SimpleHTTPServer使用指南
這篇文章主要介紹了Python內(nèi)置的HTTP協(xié)議服務(wù)器SimpleHTTPServer使用指南,SimpleHTTPServer本身的功能十分簡(jiǎn)單,文中介紹了需要的朋友可以參考下2016-03-03

