python 爬虫更换useragent后还是403,怎么解决,代码如下?

import urllib
from bs4 import BeautifulSoup
from urllib import request

head = {'User-Agent': 'Mozilla/5.0 (X11; OpenBSD i386) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'}
url = "https://book.douban.com/subject/26642302/"
request = urllib.request.Request(url, headers=head)
response = urllib.request.urlopen(request)
html = response.read().decode("utf-8")
bs = BeautifulSoup(html, 'html.parser')
web = bs.find_all(attrs={"class":{"intro"}})
for i in web:
cd = i.find_all('q')
print (cd[0].get('href'))

简单说:除了User-Agent的header之前,还可能需要其他(更有效的,能防止被对方检测出来你是爬虫)的header。但是具体是哪个,则需要你自己调试找出来才行。


具体如何调试找出来,有哪些header?

答:详见我教程(此处不给贴地址,只能给你看图,你自己搜):

温馨提示:答案为网友推荐,仅供参考
相似回答