如何针对URL验证一堆代理?

时间:2010-04-06 20:34:13

标签: python proxy curl

我有100个代理的列表。我感兴趣的网址是abc.com。我想检查可以成功获取此URL的代理数量以及相同的时间。我希望我有道理。我是一个Python noob。我正在寻找一个代码片段。非常感谢伸出援助之手:)

Proxies :

200.43.54.212
200.43.54.212
200.43.54.212
200.43.54.212

URL :

abc.com

Desired result :

Proxy          isGood Time

200.43.54.112  n      23.12  
200.43.54.222  n      12.34 
200.43.54.102  y      11.09
200.43.54.111  y       8.85

p.s:所有上述代理都有端口80或8080

1 个答案:

答案 0 :(得分:4)

您可以使用urllib2获取网址。要获得所花费的时间,您可以使用时间模块。这是一个简单的例子,可以满足您的需求:

import urllib2
import time


def testProxies(url, proxies):
    # prepare the request
    req = urllib2.Request(url)
    # run the request for each proxy
    results = ["Proxy           isGood Time"]
    for proxy in (proxies):
        # now set the proxy
        req.set_proxy(proxy, "http")
        # time it
        start = time.time()
        # try to open the URL
        try:
            urllib2.urlopen(req)
            # format the results for success
            results.append("%s  y      %.2f" % (proxy, time.time()-start))
        except urllib2.URLError:
            # format the results for failure
            results.append("%s  n      %.2f" % (proxy, time.time()-start))

    return results

testResults = testProxies("http://www.abc.com", ["200.43.54.112", "200.43.54.222",
                  "200.43.54.102", "200.43.54.111"])
for result in testResults:
    print result

要点是使用urllib2.Request(url)并使用set_proxy()功能创建请求,该功能允许您为请求设置代理。

相关问题