Python urllib2与代理

时间:2016-06-14 19:06:05

标签: python proxy urllib2 urllib

它似乎无法连接到具有代理的网站。 我试图从这个网站打印我的IP,以确认它实际上是在使用代理。

import urllib2
import re
proxy = urllib2.ProxyHandler({'http': '59.66.82.200:1080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)


f = urllib2.urlopen("http://www.canyouseeme.org/")
html_doc = f.read()
f.close()
m = re.search('(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)',html_doc)
print m.group(0)

错误:

Traceback (most recent call last):
  File "C:/Users/Skid/PycharmProjects/untitled/PureTest.py", line 8, in <module>
    f = urllib2.urlopen("http://www.canyouseeme.org/")
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 431, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 449, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1227, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1200, in do_open
    r = h.getresponse(buffering=True)
  File "C:\Python27\lib\httplib.py", line 1136, in getresponse
    response.begin()
  File "C:\Python27\lib\httplib.py", line 453, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\lib\httplib.py", line 417, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

1 个答案:

答案 0 :(得分:0)

urllib2的第154行:

返回opener.open(url,data,timeout)

尝试更改开启变量。