使用Python Requests伪装成浏览器请求百度360获取关键词批量排名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Python Requests伪装成浏览器请求百度360获取关键词批量排名相关的知识,希望对你有一定的参考价值。

#!/usr/local/python/bin/python3.7
import requests,re,time,random,string
#error=‘360搜索_访问异常出错‘ #360
error="location.href.replace" #baidu
url=["www.a.com", "www.b.com"] #你要查询的网址,可以是多个
uaList = [‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘,‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36‘]
with open(‘a.txt‘,‘r‘) as f: #查询的关键词保存在同一目录下a. txt
c=list(f.readlines())
for i in c:
ci=(i.rstrip(" "))
u=‘https://www.baidu.com/s?wd={}‘.format(ci)
#u=‘https://so.com/s?&q={}&pn=1‘.format(ci)
sj=random.randrange(10,15)
#time.sleep(sj)
header={‘User-Agent‘: random.choice(uaList)}
a=requests.get(u,headers=header).content.decode(‘utf-8‘)
if error in a:
print(ci,"error")
continue
lb=re.findall(r""text-decoration:none;">(.*)?;</a>",a)
strlb=‘‘.join(lb)
z="不在"
for i in url:
if i in strlb:
z="在"
break
print(ci,z)

以上是关于使用Python Requests伪装成浏览器请求百度360获取关键词批量排名的主要内容,如果未能解决你的问题,请参考以下文章

requests之网页采集器

爬虫简介和requests模块

爬虫-requests模块

Python3 爬虫实例 -- 伪装浏览器

Python3 爬虫 -- 伪装浏览器

Python 爬虫篇 - 通过urllib.request伪装成浏览器绕过反爬虫爬取网页所有连接实例演示,urllib2库的安装