簡體   English   中英

Python:urllib2和代理

[英]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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM