gpt4 book ai didi

不需要身份验证的网站上的python urllib HTTP错误

转载 作者:行者123 更新时间:2023-11-28 17:30:58 25 4
gpt4 key购买 nike

  1. 我正在尝试通过 Python 阅读这个网站:https://misc.interactivebrokers.com/cstools/contract_info/v3.9/index.php?action=Futures%20Search&entityId=a19207303&lang=en&wlId=GEN&showEntities=Y

我可以在任何浏览器上打开这个链接。无需身份验证。但我无法用 Python 阅读它。这是我的代码

import urllib.request as web

ibweb = 'https://misc.interactivebrokers.com/cstools/contract_info/v3.9/index.php?' + \
'action=Futures%20Search&entityId=a19207303&lang=en&wlId=GEN&showEntities=Y'

scode = web.urlopen(ibweb).read()

我收到的错误是 urllib.error.HTTPError: HTTP Error 400: Bad Request。这是错误的全套跟踪簿:

Traceback (most recent call last):

File "C:\PF\WinPython-64bit-3.4.3.3\python-3.4.3.amd64\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-96-ce48d11587fe>", line 1, in <module>
web.urlopen(ibweb)

File "C:\PF\WinPython-64bit-3.4.3.3\python-3.4.3.amd64\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)

File "C:\PF\WinPython-64bit-3.4.3.3\python-3.4.3.amd64\lib\urllib\request.py", line 469, in open
response = meth(req, response)

File "C:\PF\WinPython-64bit-3.4.3.3\python-3.4.3.amd64\lib\urllib\request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)

File "C:\PF\WinPython-64bit-3.4.3.3\python-3.4.3.amd64\lib\urllib\request.py", line 507, in error
return self._call_chain(*args)

File "C:\PF\WinPython-64bit-3.4.3.3\python-3.4.3.amd64\lib\urllib\request.py", line 441, in _call_chain
result = func(*args)

File "C:\PF\WinPython-64bit-3.4.3.3\python-3.4.3.amd64\lib\urllib\request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)

urllib.error.HTTPError: HTTP Error 400: Bad Request

当我可以在任何浏览器上打开此链接时,为什么会出现此错误?

  1. 当我尝试阅读此链接时遇到类似的错误:http://www.cboe.com/delayedquote/ssfquote.aspx ,虽然不确定是否出于同样的原因。

最佳答案

那么,为什么不使用 requests而不是 urllib?

>>> ibweb = 'https://misc.interactivebrokers.com/cstools/contract_info/v3.9/index.php?' + \
'action=Futures%20Search&entityId=a19207303&lang=en&wlId=GEN&showEntities=Y'

>>> import requests
>>> requests.get(ibweb)
<Response [200]>
>>>

关于不需要身份验证的网站上的python urllib HTTP错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34406573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com