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

五迷三道 提交于 2020-03-08 22:56:12

#!/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("\n"))
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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!