【问题标题】:use proxy in python to fetch a webpage [duplicate]在python中使用代理来获取网页[重复]
【发布时间】:2010-03-27 00:33:19
【问题描述】:

我正在尝试用 Python 编写一个函数来使用公共匿名代理并获取网页,但我遇到了一个相当奇怪的错误。
代码(我有 Python 2.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

错误:

Traceback(最近一次调用最后一次):

文件“./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-bin/teste5.py”,第 18 行,在 get_source_html_proxy 袜子=urllib2.urlopen(req)
文件“/usr/lib64/python2.4/urllib2.py”,第 130 行,在 urlopen 返回 _opener.open(url, 数据)
文件“/usr/lib64/python2.4/urllib2.py”,第 358 行,打开 response = self._open(req, data)
_open 中的文件“/usr/lib64/python2.4/urllib2.py”,第 376 行 '_open',请求)
_call_chain 中的文件“/usr/lib64/python2.4/urllib2.py”,第 337 行 结果 = func(*args)
文件“/usr/lib64/python2.4/urllib2.py”,第 573 行,在 lambda r, proxy=url, type=type, meth=self.proxy_open: \
proxy_open 中的文件“/usr/lib64/python2.4/urllib2.py”,第 580 行 如果 '@' 在主机中:
类型错误:需要可迭代参数

我不知道为什么字符“@”是一个问题(我的代码中没有这个。我应该有吗?)
提前感谢您的宝贵帮助。

【问题讨论】:

    标签: python proxy


    【解决方案1】:

    urllib2.build_opener 采用 list 处理程序

    opener = urllib2.build_opener([proxy_handler])
    

    【讨论】:

    • 谢谢。我需要了解更多。到目前为止,我从这里、从那里获取了代码,但从未按应有的方式描述。我需要回归基础。
    【解决方案2】:

    @ 本身是一个红鲱鱼,回溯来自它试图执行 x in host 操作的事实,在这种情况下,这意味着 host 必须是可迭代的(例如字符串) .你会想在那里检查host 的值,它类似于None 或一个数字,而不是你的意思。

    【讨论】:

    • 谢谢,但是什么主机? IP:代理端口?还是网址?
    • 调试器可以在回溯中显示更多详细信息。尝试 winpdb、Wing IDE 或 ipython(使用 %xmode verbose%debug
    • 其他一些来源表明存在问题:proxy_handler = urllib2.ProxyHandler({'http': pip}) 因为它缺少“http://”所以,它似乎应该是: proxy_handler = urllib2.ProxyHandler({'http': 'http://' + pip})
    猜你喜欢
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2016-05-30
    • 2012-02-27
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 2011-05-12
    相关资源
    最近更新 更多