2016-09-26 122 views
0

我有一个网页(http://rating.chgk.info/api/tournaments/3506/)我想通过urllib2在Python 2中打开。它打开非常清楚在我的浏览器,但是当我这样做:python urllib2.urlopen返回错误500,Chrome浏览器加载页面

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

我得到HTTP错误500

我试图调整的User-Agent和Accept标头,但没有奏效。还有什么可以成为问题?

+2

链接是不是为我工作:) –

回答

1

您需要先访问该网站上的页面以获取会话cookie设置:

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 
相关问题