我有检查网站的程序,我想知道如何通过Python中的代理检查它…
这是代码,例如
while True: try: h = urllib.urlopen(website) break except: print '['+time.strftime('%Y/%m/%d %H:%M:%S')+'] '+'ERROR. Trying again in a few seconds...' time.sleep(5)
默认情况下,urlopen使用环境变量http_proxy来确定要使用的HTTP代理:
urlopen
http_proxy
$ export http_proxy='http://myproxy.example.com:1234' $ python myscript.py # Using http://myproxy.example.com:1234 as a proxy
如果您想在应用程序内部指定代理,则可以给以下proxies参数提供一个参数urlopen:
proxies
proxies = {'http': 'http://myproxy.example.com:1234'} print("Using HTTP proxy %s" % proxies['http']) urllib.urlopen("http://www.google.com", proxies=proxies)
编辑: 如果我正确理解您的评论,则您想尝试多个代理并在尝试时打印每个代理。这样的事情怎么样?
candidate_proxies = ['http://proxy1.example.com:1234', 'http://proxy2.example.com:1234', 'http://proxy3.example.com:1234'] for proxy in candidate_proxies: print("Trying HTTP proxy %s" % proxy) try: result = urllib.urlopen("http://www.google.com", proxies={'http': proxy}) print("Got URL using proxy %s" % proxy) break except: print("Trying next proxy in 5 seconds") time.sleep(5)