猿问

使用 urlopen 时出现 HTTP 406 Not Acceptable 客户端错误

我正在使用urllib.request.urlopen查询 URL http://dblp.org/db/conf/lak/index。由于某种原因,我无法使用 Python 模块urllib 访问该站点,因为我收到以下 HTTP 状态代码错误:


HTTPError:HTTP 错误 406:不可接受


这是我用来发出此请求的代码:


from urllib.request import urlopen

from bs4 import BeautifulSoup


url = 'http://dblp.org/db'

html = urlopen(url).read()

soup = BeautifulSoup(html)

print(soup.prettify())

我不确定导致此错误的原因,我需要帮助来解决此错误。


以下是与此错误相关的堆栈跟踪:

HTTPError                                 Traceback (most recent call last)

<ipython-input-5-b158a1e893a0> in <module>

----> 1 html = urlopen("https://dblp.org/db").read()

      2 #print(html)

      3 soup = BeautifulSoup(html)

      4 soup.prettify()


~\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)

    220     else:

    221         opener = _opener

--> 222     return opener.open(url, data, timeout)

    223 

    224 def install_opener(opener):


~\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)

    529         for processor in self.process_response.get(protocol, []):

    530             meth = getattr(processor, meth_name)

--> 531             response = meth(req, response)

    532 

    533         return response


~\Anaconda3\lib\urllib\request.py in http_response(self, request, response)

    639         if not (200 <= code < 300):

    640             response = self.parent.error(

--> 641                 'http', request, response, code, msg, hdrs)

    642 

    643         return response


~\Anaconda3\lib\urllib\request.py in error(self, proto, *args)

    567         if http_err:

    568             args = (dict, 'default', 'http_error_default') + orig_args

--> 569             return self._call_chain(*args)

    570 

    571 # XXX probably also want an abstract factory that knows when it makes



LEATH
浏览 127回答 1
1回答

慕虎7371278

我正在研究406 错误代码,当服务器无法使用请求中指定的接受标头进行响应时,就会发生这种情况。如果我能让urlopen正常工作,我也会发布这个答案。使用Python 请求时我没有收到此错误import requestsfrom bs4 import BeautifulSoupuser_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 Requestfrom urllib.request import urlopenfrom bs4 import BeautifulSoupraw_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())
随时随地看视频慕课网APP

相关分类

Python
我要回答