Python urllib使用代理
- 免費(fèi)代理:
當(dāng)使用代理的,我們需要重寫build_opener方法
import urllib.request
proxies = {
'HTTP': '27.11.205.43:8118',
'HTTP': '120.132.116.81:8080'
}
url = 'http://www.baidu.com'
# proxies必須是一個(gè)映射關(guān)系
proxy_handler = urllib.request.ProxyHandler(proxies=proxies)
bulid_opener = urllib.request.build_opener(proxy_handler)
response = bulid_opener.open(url)
import urllib.request
proxies = {
# 當(dāng)你購買完畢以后會(huì)返回一個(gè)賬號(hào)和密碼
# 格式為賬號(hào):密碼:IP:端口
'HTTP': 'accout:passwd@ip:端口'
}
url = 'http://www.baidu.com'
# proxies必須是一個(gè)映射關(guān)系
proxy_handler = urllib.request.ProxyHandler(proxies=proxies)
bulid_opener = urllib.request.build_opener(proxy_handler)
response = bulid_opener.open(url)