2016-09-26 121 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 
相關問題