uday
uday

Reputation: 6713

python urllib HTTP Error on website that doesn't require authentication

  1. I am try to read this website via Python: https://misc.interactivebrokers.com/cstools/contract_info/v3.9/index.php?action=Futures%20Search&entityId=a19207303&lang=en&wlId=GEN&showEntities=Y

I can open this link on any browser. No authentication is required. But I am not able to read it in Python. Here's my code

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()

The error I get is urllib.error.HTTPError: HTTP Error 400: Bad Request. Here is the full set of tracebook for the error:

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

Why am I getting this error, when I can open this link on any browser?

  1. I get a similar error when I try to read this link: http://www.cboe.com/delayedquote/ssfquote.aspx, although not sure if it is for the same reason.

Upvotes: 3

Views: 195

Answers (1)

Remi Guan
Remi Guan

Reputation: 22282

Well, why don't use requests instead of 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]>
>>> 

Upvotes: 2

Related Questions