如何在Python中通过代理打开带有urllib的网站?

Bru*_*dy' 28 python proxy

我有这个检查网站的程序,我想知道如何通过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)
Run Code Online (Sandbox Code Playgroud)

Pär*_*der 39

默认情况下,urlopen使用环境变量http_proxy来确定要使用的HTTP代理:

$ export http_proxy='http://myproxy.example.com:1234'
$ python myscript.py  # Using http://myproxy.example.com:1234 as a proxy
Run Code Online (Sandbox Code Playgroud)

如果您想在应用程序中指定代理,可以给出一个proxies参数urlopen:

proxies = {'http': 'http://myproxy.example.com:1234'}
print "Using HTTP proxy %s" % proxies['http']
urllib.urlopen("http://www.google.com", proxies=proxies)
Run Code Online (Sandbox Code Playgroud)

编辑:如果我正确理解您的评论,您想尝试几个代理并在尝试时打印每个代理.这样的事怎么样?

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)
Run Code Online (Sandbox Code Playgroud)


Dom*_*Cat 28

Python 3在这里略有不同.它将尝试自动检测代理设置,但如果您需要特定或手动代理设置,请考虑这种代码:

#!/usr/bin/env python3
import urllib.request

proxy_support = urllib.request.ProxyHandler({'http' : 'http://user:pass@server:port', 
                                             'https': 'https://...'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)

with urllib.request.urlopen(url) as response:
    # ... implement things such as 'html = response.read()'
Run Code Online (Sandbox Code Playgroud)

另请参阅Python 3文档中的相关部分


Le *_*hau 6

这里的示例代码指导如何使用 urllib 通过代理连接:

authinfo = urllib.request.HTTPBasicAuthHandler()

proxy_support = urllib.request.ProxyHandler({"http" : "http://ahad-haam:3128"})

# build a new opener that adds authentication and caching FTP handlers
opener = urllib.request.build_opener(proxy_support, authinfo,
                                     urllib.request.CacheFTPHandler)

# install it
urllib.request.install_opener(opener)

f = urllib.request.urlopen('http://www.google.com/')
"""
Run Code Online (Sandbox Code Playgroud)