简体   繁体   English

Python:urllib2和代理

[英]Python: urllib2 and proxies

I'm trying to write a script in Python that reloads a page every x seconds using a list of proxies, and I'm having an issue at the moment. 我正在尝试用Python编写一个脚本,使用代理列表每x秒重新加载一个页面,我现在遇到了问题。 I know it's not the proxies' faults either, because I can ping them and they return fine. 我知道这也不是代理人的错,因为我可以ping他们并且他们返回正常。 They are HTTP proxies. 它们是HTTP代理。 My script returns this error to me: 我的脚本将此错误返回给我:

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

I have no idea how to fix it. 我不知道如何解决它。 Here is the actual script: 这是实际的脚本:

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))

Thanks. 谢谢。

Don't use urllib2 . 不要使用urllib2 Seriously, just don't. 说真的,就是不要。

Your holy grail: requests . 你的圣杯: requests

what you're trying to do is then: 那你要做的是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM