使用python抓取百度搜索结果时不成功,怎么回事? 5
如题,运行后显示为“百度安全验证的页面”!我又用了自建opener和更换UA的方式运行,同样显示这个页面?请问是什么原因?该如何解决?请各位老师给予解答,谢谢!!!...
如题,运行后显示为“百度安全验证的页面”!我又用了自建opener和更换UA的方式运行,同样显示这个页面?请问是什么原因?该如何解决?请各位老师给予解答,谢谢!!!
展开
5个回答
展开全部
异步加载(Ajax)动态渲染的网页,可能还要调用js函数去解密返回结果。如果用模拟浏览器(selemium库)获取渲染后的网页源码,难度会低很多。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2019-12-17 · 为跨境电商解答税务|商标|专利|等问题
鼎卖网
鼎卖网,跨境电商服务专家,致力服务(英法德意西)VAT注册申报、美国税务服务及全球商标专利申请,美国|香港|印度等公司注册、香港/美国银行开户、亚马逊开店、德国新包装法|WEEE认证等服务
向TA提问
关注
展开全部
网络不给力是安全验证了,我也在学,不过问题我已经解决,你要根据你正常浏览器里的 header 添加,如果实在不会,那就把出现的 所有 header 内容加进去,下面是我测试写的代码,我这边是可以采集的,50多页完全没问题,不会出现网络不给力,自己参考下
import urllib.request
import re
keywords = 'python爬虫'
keywords = urllib.request.quote(keywords)
pages = 0 #页码数
pat_res = [] #定义集合,接收采集标题
fh = open('spider\\title.txt','a') #打开文件,以追加方式
for i,j in enumerate(range(0,50)):
url = 'https://www.baidu.com/s?wd=' + keywords + '&pn='+str(pages)+'&oq=' + keywords + '&ie=utf-8&usm=2&rsv_idx=1&rsv_pq=e4f7528300143ded&rsv_t=f9e5fE0a3Gy7yaLQX6JFJhnR6I1jYnAfL2t5SCcGSoJSAr3LogHutYZxP%2FY'
opener = urllib.request.build_opener()
Bdpagetype = ('Bdpagetype', '2')
Bdqid = ('Bdqid', '0xe4fd4d4f003005ff')
Cache_Control = ('Cache-Control', 'private')
Ckpacknum = ('Ckpacknum', '2')
Ckrndstr = ('Ckrndstr', 'f003005ff')
Connection = ('Connection', 'Keep-Alive')
Content_Encoding = ('Content-Encoding', 'gzip')
Content_Type = ('Content-Type', 'text/html;charset=utf-8')
Strict_Transport_Security = ('Strict-Transport-Security', 'max-age=172800')
Traceid = ('Traceid', '1576546102028799668216500429611456267775')
Transfer_Encoding = ('Transfer-Encoding', 'chunked')
Vary = ('Vary', 'Accept-Encoding')
X_Ua_Compatible = ('X-Ua-Compatible', 'IE=Edge,chrome=1')
Accept = ('Accept',
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9')
Referer = ('Referer', 'https://www.baidu.com')
Sec_Fetch_Mode = ('Sec-Fetch-Mode', 'navigate')
Sec_Fetch_Site = ('Sec-Fetch-Site', 'same-origin')
Sec_Fetch_User = ('Sec-Fetch-User', '?1')
Upgrade_Insecure_Requests = ('Upgrade-Insecure-Requests', '1')
User_Agent = ('User-Agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edg/79.0.309.51')
opener.addheaders = [Bdpagetype]
opener.addheaders = [Bdqid]
opener.addheaders = [Cache_Control]
opener.addheaders = [Ckpacknum]
opener.addheaders = [Ckrndstr]
opener.addheaders = [Connection]
opener.addheaders = [Content_Encoding]
opener.addheaders = [Content_Type]
opener.addheaders = [Strict_Transport_Security]
opener.addheaders = [Traceid]
opener.addheaders = [Transfer_Encoding]
opener.addheaders = [Vary]
opener.addheaders = [X_Ua_Compatible]
opener.addheaders = [Accept]
opener.addheaders = [Referer]
opener.addheaders = [Sec_Fetch_Mode]
opener.addheaders = [Sec_Fetch_Site]
opener.addheaders = [Sec_Fetch_User]
opener.addheaders = [Upgrade_Insecure_Requests]
opener.addheaders = [User_Agent]
print('正在爬第:'+str(i)+'页')
data = opener.open(url)
print('当前地址:'+str(data.geturl()))
code = data.read().decode('utf-8')
pat = 'data-tools=\'{"title":"(.*?)"' #提取标题正则
print('成功获取数据,等待入库')
res_temp = re.compile(pat).findall(code)
pat_res += res_temp
print('添加入库成功')
pages = pages+10
print('采集完毕,等待写出文件...')
for w,j in enumerate(pat_res):
fh.write(str(pat_res[w])+'\n')
print('写入文件完毕!')
fh.close()
import urllib.request
import re
keywords = 'python爬虫'
keywords = urllib.request.quote(keywords)
pages = 0 #页码数
pat_res = [] #定义集合,接收采集标题
fh = open('spider\\title.txt','a') #打开文件,以追加方式
for i,j in enumerate(range(0,50)):
url = 'https://www.baidu.com/s?wd=' + keywords + '&pn='+str(pages)+'&oq=' + keywords + '&ie=utf-8&usm=2&rsv_idx=1&rsv_pq=e4f7528300143ded&rsv_t=f9e5fE0a3Gy7yaLQX6JFJhnR6I1jYnAfL2t5SCcGSoJSAr3LogHutYZxP%2FY'
opener = urllib.request.build_opener()
Bdpagetype = ('Bdpagetype', '2')
Bdqid = ('Bdqid', '0xe4fd4d4f003005ff')
Cache_Control = ('Cache-Control', 'private')
Ckpacknum = ('Ckpacknum', '2')
Ckrndstr = ('Ckrndstr', 'f003005ff')
Connection = ('Connection', 'Keep-Alive')
Content_Encoding = ('Content-Encoding', 'gzip')
Content_Type = ('Content-Type', 'text/html;charset=utf-8')
Strict_Transport_Security = ('Strict-Transport-Security', 'max-age=172800')
Traceid = ('Traceid', '1576546102028799668216500429611456267775')
Transfer_Encoding = ('Transfer-Encoding', 'chunked')
Vary = ('Vary', 'Accept-Encoding')
X_Ua_Compatible = ('X-Ua-Compatible', 'IE=Edge,chrome=1')
Accept = ('Accept',
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9')
Referer = ('Referer', 'https://www.baidu.com')
Sec_Fetch_Mode = ('Sec-Fetch-Mode', 'navigate')
Sec_Fetch_Site = ('Sec-Fetch-Site', 'same-origin')
Sec_Fetch_User = ('Sec-Fetch-User', '?1')
Upgrade_Insecure_Requests = ('Upgrade-Insecure-Requests', '1')
User_Agent = ('User-Agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edg/79.0.309.51')
opener.addheaders = [Bdpagetype]
opener.addheaders = [Bdqid]
opener.addheaders = [Cache_Control]
opener.addheaders = [Ckpacknum]
opener.addheaders = [Ckrndstr]
opener.addheaders = [Connection]
opener.addheaders = [Content_Encoding]
opener.addheaders = [Content_Type]
opener.addheaders = [Strict_Transport_Security]
opener.addheaders = [Traceid]
opener.addheaders = [Transfer_Encoding]
opener.addheaders = [Vary]
opener.addheaders = [X_Ua_Compatible]
opener.addheaders = [Accept]
opener.addheaders = [Referer]
opener.addheaders = [Sec_Fetch_Mode]
opener.addheaders = [Sec_Fetch_Site]
opener.addheaders = [Sec_Fetch_User]
opener.addheaders = [Upgrade_Insecure_Requests]
opener.addheaders = [User_Agent]
print('正在爬第:'+str(i)+'页')
data = opener.open(url)
print('当前地址:'+str(data.geturl()))
code = data.read().decode('utf-8')
pat = 'data-tools=\'{"title":"(.*?)"' #提取标题正则
print('成功获取数据,等待入库')
res_temp = re.compile(pat).findall(code)
pat_res += res_temp
print('添加入库成功')
pages = pages+10
print('采集完毕,等待写出文件...')
for w,j in enumerate(pat_res):
fh.write(str(pat_res[w])+'\n')
print('写入文件完毕!')
fh.close()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
重搜索一下,提取下验证码试一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把https改成http试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询