介绍
今天就跟大家聊聊有关怎么利用python爬虫抓取指数,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
今天方法如下:
import 请求 import 系统 import 时间 时间=word_url & # 39; http://index.baidu.com/api/SearchApi/thumbnail?area=0&词={}& # 39; 时间=COOKIES & # 39; & # 39; headers =, { ,& # 39;接受# 39;:,& # 39;application/json,文本/平原,*/* & # 39; ,& # 39;Accept-Encoding& # 39;:, & # 39; gzip、,缩小# 39; ,& # 39;接收语言# 39;:,& # 39;应用,zh型;q=0.9 & # 39; ,& # 39;cache - control # 39;:, & # 39; no - cache # 39; ,& # 39;饼干# 39;:,饼干, ,& # 39;dnt # 39;:, & # 39; 1 & # 39; ,& # 39;主机# 39;:,& # 39;index.baidu.com& # 39; ,& # 39;编译指示# 39;:,& # 39;no - cache # 39; ,& # 39;Proxy-Connection& # 39;:, & # 39;保活# 39; ,& # 39;推荐人# 39;:,& # 39;http://index.baidu.com/v2/main/index.html& # 39; ,& # 39;用户代理# 39;:,& # 39;Mozilla/5.0, (Macintosh;, Intel Mac OS X 10 _13_6), AppleWebKit/537.36, (KHTML, like 壁虎),Chrome/75.0.3770.90 Safari/537.36 & # 39; ,& # 39;X-Requested-With& # 39;:, & # 39; xmlhttprequest # 39; } def 解密(t, e):=,n 列表(t) ,小姐:=,(e)列表=,a , {}=,result []=,int, ln  (len (n)/2)=,n, start  (ln:) ,最终获得=,n (ln): ,for j, k 拷贝zip(开始,结束): a.update才能(j}{凯西:,) ,for j e:拷贝 result.append才能(a.get (j)) ,return & # 39; & # 39; . join(结果) ,, def get_ptbk(函数):=,url & # 39; http://index.baidu.com/Interface/ptbk?uniqid={} & # 39;=,,resp  requests.get (url.format(函数),标题=标题) ,if resp.status_code  !=, 200: 打印才能(& # 39;获函数取失败& # 39;) sys.exit才能(1) ,return resp.json () . get(& # 39;数据# 39;) def get_index_data(关键字,,开始=& # 39;2011 - 01 - 03 & # 39;,,结束=& # 39;2019 - 08 - 05 - & # 39;):=,,keyword  str(关键字).replace (“& # 39;“,, & # 39;“& # 39;)=,url f # 39; http://index.baidu.com/api/SearchApi/index?area=0&词={关键词},面积=0,startDate可以={开始},endDate={结束}& # 39;=,,resp  requests.get (url,头=标题) 打印才能(& # 39;获取指数失败& # 39;)=,,content  resp.json ()=,,data  content.get(& # 39;数据# 39;)=,,user_indexes  data.get (& # 39; userIndexes& # 39;) [0]=,,uniqid  data.get(& # 39;函数# 39;)=,,ptbk  get_ptbk(函数) ,while ptbk  is None 或是ptbk ==, & # 39; & # 39;: 时间=ptbk 才能;get_ptbk(函数)=,,all_data  user_indexes.get(& # 39;所有# 39;). get(& # 39;数据# 39;) ,result =,解密(ptbk, all_data)=,,result  result.split (& # 39; & # 39;) ,打印(结果) if __name__ ==, & # 39; __main__ # 39;:=,words [[{“name":,“酷安“,,“wordType":, 1}]] get_index_data(单词)
输出: