python爬蟲中抓取指數(shù)的實(shí)例講解
有一些數(shù)據(jù)我們是沒法直觀的查看的,需要通過抓取去獲得。聽到指數(shù)這個(gè)詞,有的小伙伴們覺得很復(fù)雜,似乎只在股票的時(shí)候才聽說的,比如一些數(shù)據(jù)的漲跌分析都是比較棘手的問題。不過指數(shù)對(duì)于我們的數(shù)據(jù)分析還是很有幫助的,今天小編就python爬蟲中抓取指數(shù)得方法給大家?guī)碇v解。
剛好這幾天需要用到這個(gè)爬蟲,結(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)然也可以用來看股票以及其他的一些操作,運(yùn)用python爬蟲解決都是不錯(cuò)的選擇,感興趣的小伙伴也可以跟著小編嘗試一下。
到此這篇關(guān)于python爬蟲中抓取指數(shù)的實(shí)例講解的文章就介紹到這了,更多相關(guān)python爬蟲中如何抓取指數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python實(shí)現(xiàn)的爬取百度文庫功能示例
- 用python下載百度文庫的代碼
- python爬蟲爬取淘寶商品比價(jià)(附淘寶反爬蟲機(jī)制解決小辦法)
- python爬蟲多次請(qǐng)求超時(shí)的幾種重試方法(6種)
- python爬蟲搭配起B(yǎng)ilibili唧唧的流程分析
- 用sleep間隔進(jìn)行python反爬蟲的實(shí)例講解
- celery在python爬蟲中定時(shí)操作實(shí)例講解
- Python爬蟲框架Scrapy安裝使用步驟
- 使用Python編寫簡單網(wǎng)絡(luò)爬蟲抓取視頻下載資源
- 零基礎(chǔ)寫python爬蟲之使用Scrapy框架編寫爬蟲
- 零基礎(chǔ)寫python爬蟲之使用urllib2組件抓取網(wǎng)頁內(nèi)容
- python 爬取百度文庫并下載(免費(fèi)文章限定)
相關(guān)文章
Python通過matplotlib繪制動(dòng)畫簡單實(shí)例
這篇文章主要介紹了Python通過matplotlib繪制動(dòng)畫簡單實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
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內(nèi)置的HTTP協(xié)議服務(wù)器SimpleHTTPServer使用指南
這篇文章主要介紹了Python內(nèi)置的HTTP協(xié)議服務(wù)器SimpleHTTPServer使用指南,SimpleHTTPServer本身的功能十分簡單,文中介紹了需要的朋友可以參考下2016-03-03

