from urllib.error import URLErrorfrom urllib.request import ProxyHandler, build_openerproxy = 127.0.0.1:9743proxy_handler = ProxyHandler({ http: http:// + proxy, https: https:// + proxy})opener = build_opener(proxy_handler)try: response = opener.open( print(response.read().decode(utf-8))except URLError as e: print(e.reason)
运行结果如下:
{ "args": {}, "headers": { "Accept-Encoding": "identity", "Connection": "close", "Host": "httpbin.org", "User-Agent": "Python-urllib/3.6" }, "origin": "106.185.45.153", "url": ""}
from urllib.error import URLErrorfrom urllib.request import ProxyHandler, build_openerproxy = username:[email protected]:9743proxy_handler = ProxyHandler({ http: http:// + proxy, https: https:// + proxy})opener = build_opener(proxy_handler)try: response = opener.open( print(response.read().decode(utf-8))except URLError as e: print(e.reason)
import socksimport socketfrom urllib import requestfrom urllib.error import URLErrorsocks.set_default_proxy(socks.SOCKS5, 127.0.0.1, 9742)socket.socket = socks.socksockettry: response = request.urlopen( print(response.read().decode(utf-8))except URLError as e: print(e.reason)
此处需要一个 Socks 模块,可以通过如下命令安装:
pip3 install PySocks
本地我有一个 SOCKS5 代理,运行在 9742 端口,运行成功之后和上文 HTTP 代理输出结果是一样的:
{ "args": {}, "headers": { "Accept-Encoding": "identity", "Connection": "close", "Host": "httpbin.org", "User-Agent": "Python-urllib/3.6" }, "origin": "106.185.45.153", "url": ""}
import requestsproxy = 127.0.0.1:9743proxies = { http: http:// + proxy, https: https:// + proxy,}try: response = requests.get(, proxies=proxies) print(response.text)except requests.exceptions.ConnectionError as e: print(Error, e.args)
运行结果:
{ "args": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Host": "httpbin.org", "User-Agent": "python-requests/2.18.1" }, "origin": "106.185.45.153", "url": ""}
proxy = username:[email protected]:9743
import requestsproxy = 127.0.0.1:9742proxies = { http: socks5:// + proxy, https: socks5:// + proxy}try: response = requests.get(, proxies=proxies) print(response.text)except requests.exceptions.ConnectionError as e: print(Error, e.args)
在这里需要额外安装一个 Socks 模块,命令如下:
pip3 install "requests[socks]"
运行结果是完全相同的:
{ "args": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Host": "httpbin.org", "User-Agent": "python-requests/2.18.1" }, "origin": "106.185.45.153", "url": ""}
另外还有一种设置方式,和 Urllib 中的方法相同,使用 socks 模块,也需要像上文一样安装该库,设置方法如下:
import requestsimport socksimport socketsocks.set_default_proxy(socks.SOCKS5, 127.0.0.1, 9742)socket.socket = socks.socksockettry: response = requests.get( print(response.text)except requests.exceptions.ConnectionError as e: print(Error, e.args)
from selenium import webdriverproxy = 127.0.0.1:9743chrome_options = webdriver.ChromeOptions()chrome_options.add_argument(--proxy-server=http:// + proxy)browser = webdriver.Chrome(chrome_options=chrome_options)browser.get(
{ "args": {}, "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.8", "Connection": "close", "Host": "httpbin.org", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" }, "origin": "106.185.45.153", "url": ""}
可以看到 origin 同样为代理 IP 的地址,代理设置成功。
如果代理是认证代理,则设置方法相对比较麻烦,方法如下:
from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsimport zipfileip = 127.0.0.1port = 9743username = foopassword = barmanifest_json = """{ "version": "1.0.0", "manifest_version": 2, "name": "Chrome Proxy", "permissions": [ "proxy", "tabs", "unlimitedStorage", "storage", "<all_urls>", "webRequest", "webRequestBlocking" ], "background": { "scripts": ["background.js"] }}"""background_js = """var config = { mode: "fixed_servers", rules: { singleProxy: { scheme: "http", host: "%(ip)s", port: %(port)s } } }chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});function callbackFn(details) { return { authCredentials: { username: "%(username)s", password: "%(password)s" } }}chrome.webRequest.onAuthRequired.addListener( callbackFn, {urls: ["<all_urls>"]}, [blocking])""" % {ip: ip, port: port, username: username, password: password}plugin_file = proxy_auth_plugin.zipwith zipfile.ZipFile(plugin_file, w) as zp: zp.writestr("manifest.json", manifest_json) zp.writestr("background.js", background_js)chrome_options = Options()chrome_options.add_argument("--start-maximized")chrome_options.add_extension(plugin_file)browser = webdriver.Chrome(chrome_options=chrome_options)browser.get(
from selenium import webdriverservice_args = [ --proxy=127.0.0.1:9743, --proxy-type=http]browser = webdriver.PhantomJS(service_args=service_args)browser.get(print(browser.page_source)
在这里我们只需要使用 service_args 参数,将命令行的一些参数定义为列表,在初始化的时候传递即可。
运行结果:
{ "args": {}, "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,en,*", "Connection": "close", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.0 Safari/538.1" }, "origin": "106.185.45.153", "url": ""}
运行结果的 origin 同样为代理的 IP,设置代理成功。
如果需要认证,那么只需要再加入 --proxy-auth 选项即可,这样参数就改为:
service_args = [ --proxy=127.0.0.1:9743, --proxy-type=http, --proxy-auth=username:password]
如您有何疑问,请联系小编: