2014-08-30 80 views
0

當我運行這個腳本時,我得到了很多錯誤。 進口的urllib,urllib2的爲什麼我會收到這些錯誤以及如何解決這些錯誤?

proxy = urllib2.ProxyHandler({ 
    'http': '127.0.0.1', 
    'https': '127.0.0.1' 
}) 
opener = urllib2.build_opener(proxy) 
urllib2.install_opener(opener) 
# this way both http and https requests go through the proxy 
urllib2.urlopen('http://www.google.com') 
urllib2.urlopen('https://www.google.com') 

我真的不明白這些錯誤,因此爲什麼我問。在這裏,他們是:

Traceback (most recent call last): 
    File "C:\Python27\Craig.py", line 10, in <module> 
    urllib2.urlopen('http://www.google.com') 
    File "C:\Python27\lib\urllib2.py", line 127, in urlopen 
    return _opener.open(url, data, timeout) 
    File "C:\Python27\lib\urllib2.py", line 410, in open 
    response = meth(req, response) 
    File "C:\Python27\lib\urllib2.py", line 523, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python27\lib\urllib2.py", line 448, in error 
    return self._call_chain(*args) 
    File "C:\Python27\lib\urllib2.py", line 382, in _call_chain 
    result = func(*args) 
    File "C:\Python27\lib\urllib2.py", line 531, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
urllib2.HTTPError: HTTP Error 501: Not Implemented 

更新: 後,我加入我得到這些錯誤的端口:

Traceback (most recent call last): 
    File "C:\Python27\Craig.py", line 10, in <module> 
    urllib2.urlopen('http://www.google.com') 
    File "C:\Python27\lib\urllib2.py", line 127, in urlopen 
    return _opener.open(url, data, timeout) 
    File "C:\Python27\lib\urllib2.py", line 404, in open 
    response = self._open(req, data) 
    File "C:\Python27\lib\urllib2.py", line 422, in _open 
    '_open', req) 
    File "C:\Python27\lib\urllib2.py", line 382, in _call_chain 
    result = func(*args) 
    File "C:\Python27\lib\urllib2.py", line 1214, in http_open 
    return self.do_open(httplib.HTTPConnection, req) 
    File "C:\Python27\lib\urllib2.py", line 1184, in do_open 
    raise URLError(err) 
urllib2.URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it> 
+1

你的代理服務器不處理代理請求。 – 2014-08-30 15:50:11

+0

那麼我能做些什麼來解決這個問題? – winders 2014-08-30 15:50:32

+1

確保您實際上在localhost端口80上使用代理而不是其他內容?或者,如果您的代理正在另一個端口上運行,請使用該端口?這是**不**,但是,一個Python問題。 – 2014-08-30 15:54:39

回答

0

你運行的本地代理?

如果你是,它可能不是80端口上的默認端口上運行是3128

proxy = urllib2.ProxyHandler({ 
    'http': 'http://127.0.0.1:3128/', 
    'https': 'http://127.0.0.1:3128/' 
}) 
+0

好的,它可能已經工作了,但我得到更多的錯誤,因爲你可以看到頂部。 – winders 2014-08-30 15:55:31

+0

您是否正在運行本地代理? – MattH 2014-08-30 15:56:57

+0

是的,我是。還有相同的錯誤:( – winders 2014-08-30 15:59:27