Python: urllib2 and proxies



我试图用Python编写一个脚本,使用代理列表每x秒重新加载一个页面,我现在有一个问题。我知道这也不是代理的错误,因为我可以ping它们并且它们返回正常。它们是HTTP代理。我的脚本返回这个错误给我:

urllib.error.URLError: <urlopen error [WinError 10061] No connection could be made because the target machine actively refused it>

我不知道如何修理它。下面是实际的脚本:

import urllib.request
import time
proxy_list = input("Name of proxy list file?: ")
proxy_file = open(proxy_list, 'r')
url = input("URL to bot? (Has to include http://): ")
sleep = float(input("Time between reloads? (In seconds, 0 for none): "))
proxies = []
for line in proxy_file:
    proxies.append( line )
proxies = [w.replace('n', '') for w in proxies]
while True:
    for i in range(len(proxies)):
        proxy = proxies[i]
        proxy2 = {"http":"http://%s" % proxy}
        proxy_support = urllib.request.ProxyHandler(proxy2)
        opener = urllib.request.build_opener(proxy_support)
        urllib.request.install_opener(opener)
        urllib.request.urlopen(url).read()
        time.sleep(float(sleep))

谢谢。

不要使用urllib2。说真的,千万别。

你的圣杯:requests

你要做的是:

while True:
    for proxy in proxies:
        r = request.get(my_url, proxies={'http': proxy})
        print r.text
        time.sleep(float(sleep))

最新更新