當我運行這個腳本時,我得到了很多錯誤。 進口的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>
你的代理服務器不處理代理請求。 – 2014-08-30 15:50:11
那麼我能做些什麼來解決這個問題? – winders 2014-08-30 15:50:32
確保您實際上在localhost端口80上使用代理而不是其他內容?或者,如果您的代理正在另一個端口上運行,請使用該端口?這是**不**,但是,一個Python問題。 – 2014-08-30 15:54:39