我试图用Python编写一个函数来使用一个公共匿名代理并获取一个网页,但是我遇到了一个相当奇怪的错误。
代码(我有Python2.4):
import urllib2
def get_source_html_proxy(url, pip, timeout):
# timeout in seconds (maximum number of seconds willing for the code to wait in
# case there is a proxy that is not working, then it gives up)
proxy_handler = urllib2.ProxyHandler({'http': pip})
opener = urllib2.build_opener(proxy_handler)
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib2.install_opener(opener)
req=urllib2.Request(url)
sock=urllib2.urlopen(req)
timp=0 # a counter that is going to measure the time until the result (webpage) is
# returned
while 1:
data = sock.read(1024)
timp=timp+1
if len(data) < 1024: break
timpLimita=50000000 * timeout
if timp==timpLimita: # 5 millions is about 1 second
break
if timp==timpLimita:
print IPul + ": Connection is working, but the webpage is fetched in more than 50 seconds. This proxy returns the following IP: " + str(data)
return str(data)
else:
print "This proxy " + IPul + "= good proxy. " + "It returns the following IP: " + str(data)
return str(data)
# Now, I call the function to test it for one single proxy (IP:port) that does not support user and password (a public high anonymity proxy)
#(I put a proxy that I know is working - slow, but is working)
rez=get_source_html_proxy("http://www.whatismyip.com/automation/n09230945.asp", "93.84.221.248:3128", 50)
print rez
错误:
回溯(最近一次调用):
文件"./public_html/cgi-bin/teste5.py",第43行,在?
rez=get_source_html_proxy("http://www.whatismyip.com/automation/n09230945.asp","xx.yy.zzz.ww:3128",50)
文件“./public_html/cgi/teste5.py”,第18行,在get_source_html_proxy sock=urllib2.urlopen(req)中
文件“/usr/lib64 64/python2.4/urllib2.py”,第130行,在urlopen返回_opener.open(url,data)中
文件“/usr/lib64 64/python2.4/urllib2.py”,第358行,开放响应= self._open(req,data)
文件“/usr/lib64 64/python2.4/urllib2.py”,第376行,在_open '_open‘中,req)
文件“/usr/lib64 64/python2.4/urllib2.py”,第337行,在_call_chain result = func(*args)中
文件“/usr/lib64 64/python2.4/urllib2.py”,第573行,在lambda r、proxy=url、type=type、meth=self.proxy_open中:\
文件“/usr/lib64 64/python2.4/urllib2.py”,第580行,proxy_open if '@‘in host:
TypeError:迭代参数需要
我不知道为什么字符"@“是一个问题(我没有这样的代码。我应该这么做吗?)
提前感谢您的宝贵帮助。
发布于 2010-03-26 17:51:31
urllib2.build_opener接受一个处理程序列表
opener = urllib2.build_opener([proxy_handler])
发布于 2010-03-26 16:41:30
@
本身就是一个红鲱鱼,追溯来自于它试图执行x in host
操作的事实,在这种情况下,这意味着host
必须是可迭代的(例如字符串)。您需要检查host
的值,它类似于None
或数字,而不是您的意思。
https://stackoverflow.com/questions/2527668
复制相似问题