urllib代理设置:
from urllib.error import URLErrorfrom urllib.request import ProxyHandler,build_openerproxy='192.168.xxx.xxx' #使用本地代理proxy_handl=ProxyHandler({ 'http':'http://'+proxy, 'https':'https://'+proxy})opener=build_opener(proxy_handl)try: response=opener.open('http://httpbin.org/get') #测试网址 print(response.read().decode('utf-8'))except URLError as e: print(e.reason)
requests代理设置
import requestsproxy='192.168.xx.xx:8080' #本地代理proxies={ 'http':'http://'+proxy, 'https':'https://'+proxy}try: response=requests.get('http://baidu.com',proxies=proxies) print(response.text)except requests.exceptions.ConnectionError as e: print(e.args)
Selenium代理设置:
from selenium import webdriverproxy='123.xx.xx.xx:8080'#代理地址chrome_options=webdriver.ChromeOptions()chrome_options.add_argument('--proxy-server=http://'+proxy)browser=webdriver.Chrome(chrome_options=chrome_options)browser.get('url') #url为网址