Search code examples
pythonweb-scrapingurllib2

python urllib2.urlopen returns error 500 while Chrome loads the page


I have a web-page (http://rating.chgk.info/api/tournaments/3506/) I want to open in Python 2 via urllib2. It opens perfectly well in my browser but when I do this:

import urllib2
url = 'http://rating.chgk.info/api/tournaments/3506/'
urllib2.urlopen(url)

i get HTTP Error 500.

I tried tweaking User-Agent and Accept headers but nothing worked. What else can be the problem?


Solution

  • You need to first visit the page on the site to get the session cookie set:

    In [7]: import requests
    
    In [8]: requests.get("http://rating.chgk.info/api/tournaments/3506")
    Out[8]: <Response [500]>
    
    In [9]: with requests.Session() as session:
        ...:     session.get("http://rating.chgk.info/index.php/api")
        ...:     response = session.get("http://rating.chgk.info/api/tournaments/3506")
        ...:     print(response.status_code)
        ...:     
    200