1. 程式人生 > >python使用代理訪問服務器

python使用代理訪問服務器

ces clas code OS hat post www. pen ner

python使用代理訪問服務器主要有一下3個步驟:

1.創建一個代理處理器ProxyHandler:

proxy_support = urllib.request.ProxyHandler(),ProxyHandler是一個類,其參數是一個字典:{ ‘類型‘:‘代理ip:端口號‘}

什麽是Handler?Handler也叫作處理器,每個handlers知道如何通過特定協議打開URLs,或者如何處理URL打開時的各個方面,例如HTTP重定向或者HTTP cookies。

2.定制、創建一個opener:

opener = urllib.request.build_opener(proxy_support)

什麽是opener?python在打開一個url鏈接時,就會使用opener。其實,urllib.request.urlopen()函數實際上是使用的是默認的opener,只不過在這裏我們需要定制一個opener來指定handler。

3a.安裝opener

urllib.request.install_opener(opener)

install_opener 用來創建(全局)默認opener,這個表示調用urlopen將使用你安裝的opener。

3b.調用opener
opener.open(url)

該方法可以像urlopen函數那樣直接用來獲取urls:通常不必調用install_opener,除了為了方便。

>>> proxy_support = urllib.request.ProxyHandler({http:115.32.41.100:80})
>>> proxy_support
<urllib.request.ProxyHandler object at 0x0000000002EE74A8>
>>> opener = urllib.request.build_opener(proxy_support)
>>> opener
<urllib.request.OpenerDirector object at 0x0000000002F972B0>
>>> opener.handlers
[
<urllib.request.ProxyHandler object at 0x0000000002EE74A8>, <urllib.request.UnknownHandler object at 0x0000000003197B38>, <urllib.request.HTTPHandler object at 0x0000000003197C18>, <urllib.request.HTTPDefaultErrorHandler object at 0x0000000003197CC0>, <urllib.request.HTTPRedirectHandler object at 0x0000000003197BA8>, <urllib.request.FTPHandler object at 0x0000000003197DD8>, <urllib.request.FileHandler object at 0x0000000003197E80>, <urllib.request.HTTPSHandler object at 0x0000000003197E48>, <urllib.request.HTTPErrorProcessor object at 0x0000000003197E10>] >>> opener.addheaders [(User-agent, Python-urllib/3.3)] >>> opener.addheaders = [(User-Agent,Test_Proxy_Python3.5_maminyao)] >>> opener.addheaders [(User-Agent, Test_Proxy_Python3.5_maminyao)] >>>

從代理ip列表中隨機使用某ip去訪問URL的例子

 1 import urllib.request
 2 import random
 3 
 4 url = http://www.whatismyip.com.tw
 5 iplist = [115.32.41.100:80,58.30.231.36:80,123.56.90.175:3128]
 6 
 7 proxy_support = urllib.request.ProxyHandler({http:random.choice(iplist)})
 8 opener = urllib.request.build_opener(proxy_support)
 9 opener.addheaders = [(User-Agent,Test_Proxy_Python3.5_maminyao)]
10 urllib.request.install_opener(opener)
11 response = urllib.request.urlopen(url)
12 html = response.read().decode(utf-8)
13 
14 print(html)

python使用代理訪問服務器