我可以在任何浏览器上打开此链接。无需身份验证。但是我无法用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
。以下是错误的全套tracebook:
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
当我可以在任何浏览器上打开此链接时,为什么会出现此错误?
答案 0 :(得分:2)
那么,为什么不使用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]>
>>>