2010-07-02 26 views
15

tôi có chương trình này mà kiểm tra một trang web, và tôi muốn biết làm thế nào tôi có thể kiểm tra xem nó thông qua proxy trong Python ...Làm cách nào để mở một trang web với urllib qua proxy trong Python?

này là mã, chỉ ví dụ

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

urllib2 http://stackoverflow.com/questions/1450132/proxy-with-urllib2 –

Trả lời

29

Theo mặc định, urlopen sử dụng biến môi trường http_proxy để xác định HTTP proxy để sử dụng:

$ export http_proxy='http://myproxy.example.com:1234' 
$ python myscript.py # Using http://myproxy.example.com:1234 as a proxy 

Nếu bạn thay vì muốn chỉ định một proxy bên trong ứng dụng của bạn, bạn có thể cho một ar proxies gument để urlopen:

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

Edit: Nếu tôi hiểu ý kiến ​​của bạn một cách chính xác, bạn muốn thử một vài proxy và in mỗi proxy khi bạn thử nó. Còn những thứ như thế này thì sao?

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

sử dụng ví dụ của bạn, làm cách nào tôi có thể in proxy nào đang sử dụng trong thời gian urlopen xảy ra? – Shady

+0

@Shady: Chỉ cần ném vào câu lệnh 'print' để in giá trị của' proxy ['http'] '. Hãy xem ví dụ được cập nhật của tôi để xem nó có thể được thực hiện như thế nào. –

+0

ok cảm ơn, nhưng nếu tôi muốn nhiều proxy, như, tấn của nó, ví dụ 10 proxy, mở một trước khi một tiếp theo – Shady

0

Đây mã ví dụ hướng dẫn làm thế nào để sử dụng urllib để kết nối qua proxy:

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/') 
""" 
15

Python 3 là hơi khác nhau ở đây. Nó sẽ cố gắng để tự động phát hiện thiết lập proxy nhưng nếu bạn cần cài đặt proxy cụ thể hoặc bằng tay, suy nghĩ về loại mã này:

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

proxy_support = urllib.request.ProxyHandler({'http' : 'http://user:[email protected]: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()' 

cũng Tham khảo the relevant section in the Python 3 docs

0

Đối với http và https sử dụng:

proxies = {'http':'http://proxy-source-ip:proxy-port', 
      'https':'https://proxy-source-ip:proxy-port'} 
hơn

proxy có thể được bổ sung tương tự

proxies = {'http':'http://proxy1-source-ip:proxy-port', 
      'http':'http://proxy2-source-ip:proxy-port' 
      ... 
      } 

sử dụng

filehandle = urllib.urlopen(external_url , proxies=proxies) 

Không sử dụng bất kỳ proxy (trong trường hợp của các liên kết trong mạng)

filehandle = urllib.urlopen(external_url, proxies={}) 

Sử dụng proxy xác thực thông qua tên truy cập và mật khẩu

proxies = {'http':'http://username:[email protected]:proxy-port', 
      'https':'https://username:[email protected]:proxy-port'} 

Lưu ý: tránh sử dụng ký tự đặc biệt chẳng hạn như :,@ trong tên người dùng và mật khẩu

Các vấn đề liên quan