python urllib2.urlopen returns 错误 500 而 Chrome 加载页面
python urllib2.urlopen returns error 500 while Chrome loads the page
我有一个 web-page (http://rating.chgk.info/api/tournaments/3506/) 我想通过 urllib2 在 Python 2 中打开。它在我的浏览器中打开得很好,但是当我这样做时:
import urllib2
url = 'http://rating.chgk.info/api/tournaments/3506/'
urllib2.urlopen(url)
我收到 HTTP 错误 500。
我尝试调整 User-Agent 并接受 headers 但没有任何效果。还有什么问题?
您需要先访问站点上的页面以获取会话 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
我有一个 web-page (http://rating.chgk.info/api/tournaments/3506/) 我想通过 urllib2 在 Python 2 中打开。它在我的浏览器中打开得很好,但是当我这样做时:
import urllib2
url = 'http://rating.chgk.info/api/tournaments/3506/'
urllib2.urlopen(url)
我收到 HTTP 错误 500。
我尝试调整 User-Agent 并接受 headers 但没有任何效果。还有什么问题?
您需要先访问站点上的页面以获取会话 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