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

