2016-04-03 80 views
0

我真的很困惑,新來的Python和我正在研究一個腳本,在Python27上的產品刮傷網站。我試圖使用urllib2來做到這一點,當我運行腳本時,它會打印多個追溯錯誤。建議?Python跟蹤錯誤使用urllib2

腳本:

import urllib2, zlib, json 

url='https://launches.endclothing.com/api/products' 
req = urllib2.Request(url) 
req.add_header(':host','launches.endclothing.com');req.add_header(':method','GET');req.add_header(':path','/api/products');req.add_header(':scheme','https');req.add_header(':version','HTTP/1.1');req.add_header('accept','application/json, text/plain, */*');req.add_header('accept-encoding','gzip,deflate');req.add_header('accept-language','en-US,en;q=0.8');req.add_header('cache-control','max-age=0');req.add_header('cookie','__/');req.add_header('user-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36'); 
resp = urllib2.urlopen(req).read() 
resp = zlib.decompress(bytes(bytearray(resp)),15+32) 
data = json.loads(resp) 
for product in data: 
    for attrib in product.keys(): 
     print str(attrib)+' :: '+ str(product[attrib]) 
    print '\n' 

錯誤(S):

C:\Users\Luke>py C:\Users\Luke\Documents\EndBot2.py 
Traceback (most recent call last): 
    File "C:\Users\Luke\Documents\EndBot2.py", line 5, in <module> 
    resp = urllib2.urlopen(req).read() 
    File "C:\Python27\lib\urllib2.py", line 126, in urlopen 
    return _opener.open(url, data, timeout) 
    File "C:\Python27\lib\urllib2.py", line 391, in open 
    response = self._open(req, data) 
    File "C:\Python27\lib\urllib2.py", line 409, in _open 
    '_open', req) 
    File "C:\Python27\lib\urllib2.py", line 369, in _call_chain 
    result = func(*args) 
    File "C:\Python27\lib\urllib2.py", line 1181, in https_open 
    return self.do_open(httplib.HTTPSConnection, req) 
    File "C:\Python27\lib\urllib2.py", line 1148, in do_open 
    raise URLError(err) 
urllib2.URLError: <urlopen error [Errno 1] _ssl.c:499: error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error> 
+2

它似乎在這裏運行良好。沒有錯誤。 – jDo

+0

@jDo,你在Python 2.7? – rated

+0

這發生在'HTTPS'網站上,並且表明您的計算機的SSL軟件可能已過時,或者服務器請求了您沒有的內容,如SSL證書。如果不使用'urllib',而是使用['requests'](http://docs.python-requests.org/en/master/),這會帶來更好的全面支持所有這些問題都是行業標準。 –

回答

3

你快與你的要求的SSL配置問題。我很抱歉,但我不會糾正你的代碼,因爲我們是在2016年,並有一個美好的庫,你應該使用:requests

因此它的用法很簡單:

>>> user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1' 
>>> result = requests.get('https://launches.endclothing.com/api/products', headers={'user-agent': user_agent}) 
>>> result 
<Response [200]> 
>>> result.json() 
[{u'name': u'Adidas Consortium x HighSnobiety Ultraboost', u'colour': u'Grey', u'id': 30, u'releaseDate': u'2016-04-09T00:01:00+0100', … 

你會發現,我改變了用戶代理在前面的查詢到有工作,因爲古怪的是,該網站拒絕requests API訪問:

>>> result = requests.get('https://launches.endclothing.com/api/products') 
>>> result 
<Response [403]> 
>>> result.text 
This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.</p></div><div class="error-right"><h3>What can I do to resolve this?</h3><p>If you are on a personal connection, like at home, you can run an anti-virus scan on your device to make sure it is not infected with malware.</p><p>If you are at an office or shared network, you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices. 

否則,現在您已經嘗試requests並且您的生活發生了變化,您可能仍然會遇到此問題。正如你可能從互聯網上的很多地方讀到的,這與SNI and outdated libraries有關,你可能會很頭痛,試圖解決這個問題。我最好的建議是升級到Python3,因爲問題可能通過安裝一個新的python版本和相關的庫來解決。

HTH

+0

請注意,僅僅切換到'requests'還不夠:您仍然可以獲得SSL錯誤。通常,這是由[SNI問題](https://github.com/kennethreitz/requests/issues/2022)造成的,並且可以按照其中的說明輕鬆解決。 –

+0

是的,我正要提出這一點,但我需要在發佈前多做一點google-fu☺ – zmo

+0

將其轉換爲請求!所以在這樣做時,回溯錯誤將消失? @zmo – rated