【发布时间】:2021-01-21 16:06:51
【问题描述】:
我正在使用 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
~\Anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
501 for handler in handlers:
502 func = getattr(handler, meth_name)
--> 503 result = func(*args)
504 if result is not None:
505 return result
~\Anaconda3\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs)
647 class HTTPDefaultErrorHandler(BaseHandler):
648 def http_error_default(self, req, fp, code, msg, hdrs):
--> 649 raise HTTPError(req.full_url, code, msg, hdrs, fp)
650
651 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 406: Not Acceptable
【问题讨论】:
-
如果您提供网站的网址会很有帮助
-
@Sushil 这是网址dblp.org/db/conf/lak/index
-
可能是 http 标头错误:developer.mozilla.org/en-US/docs/Web/HTTP/Status/…。响应的正文是什么?尝试使用浏览器发送的相同标头。
-
@ThomasSablik 当我正常打开它时,它工作正常,但是当我使用我的代码处理它时,它会抛出 406 错误。我已经知道 406 错误是什么,但我不知道我该怎么做在这种情况下解决它。
-
修复标题。您要么发送错误的标头,要么缺少强制性标头。
标签: python python-3.x urllib httpresponse