1 回答
TA贡献1802条经验 获得超4个赞
我正在研究406 错误代码,当服务器无法使用请求中指定的接受标头进行响应时,就会发生这种情况。如果我能让urlopen正常工作,我也会发布这个答案。
使用Python 请求时我没有收到此错误
import requests
from bs4 import BeautifulSoup
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
raw_html = requests.get('http://dblp.org/db/conf/lak/index')
soup = BeautifulSoup(raw_html.content, 'html.parser')
print(soup.prettify())
下面的答案使用urlopen,它不会产生 406 错误。
from urllib.request import Request
from urllib.request import urlopen
from bs4 import BeautifulSoup
raw_request = Request('https://dblp.org/db/conf/lak/index')
raw_request.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0')
raw_request.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
resp = urlopen(raw_request)
raw_html = resp.read()
soup = BeautifulSoup(raw_html, 'html.parser')
print(soup.prettify())
添加回答
举报