commit 3b0eda8c3efecfd2e93a999b0d340664367cec41 Author: zhongpei Date: Wed Apr 29 15:55:33 2020 +0800 init diff --git a/README.md b/README.md new file mode 100644 index 0000000..6ae23ac --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ + +#安装chrome并下载对应版本的chrome deriver + +## 下载chrome +https://www.google.com/chrome/ + +## 下载对应版本 driver +https://chromedriver.chromium.org/downloads + +# pip安装库 +pip install -r requirements.txt + +# 执行代码 +python demo.py + + + + diff --git a/demo.py b/demo.py new file mode 100644 index 0000000..a8a0cf6 --- /dev/null +++ b/demo.py @@ -0,0 +1,88 @@ +import os +import zipfile + +from selenium import webdriver + +PROXY_HOST = 'u1.5.tn.16yun.cn' # rotating proxy or host +PROXY_PORT = 6441 # port +PROXY_USER = '16ZJZYVL' # username +PROXY_PASS = '167813' # password + + +manifest_json = """ +{ + "version": "1.0.0", + "manifest_version": 2, + "name": "Chrome Proxy", + "permissions": [ + "proxy", + "tabs", + "unlimitedStorage", + "storage", + "", + "webRequest", + "webRequestBlocking" + ], + "background": { + "scripts": ["background.js"] + }, + "minimum_chrome_version":"22.0.0" +} +""" + +background_js = """ +var config = { + mode: "fixed_servers", + rules: { + singleProxy: { + scheme: "http", + host: "%s", + port: parseInt(%s) + }, + bypassList: ["localhost"] + } + }; + +chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); + +function callbackFn(details) { + return { + authCredentials: { + username: "%s", + password: "%s" + } + }; +} + +chrome.webRequest.onAuthRequired.addListener( + callbackFn, + {urls: [""]}, + ['blocking'] +); +""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS) + + +def get_chromedriver(use_proxy=False, user_agent=None): + path = os.path.dirname(os.path.abspath(__file__)) + chrome_options = webdriver.ChromeOptions() + if use_proxy: + pluginfile = 'proxy_auth_plugin.zip' + + with zipfile.ZipFile(pluginfile, 'w') as zp: + zp.writestr("manifest.json", manifest_json) + zp.writestr("background.js", background_js) + chrome_options.add_extension(pluginfile) + if user_agent: + chrome_options.add_argument('--user-agent=%s' % user_agent) + driver = webdriver.Chrome( + os.path.join(path, 'chromedriver'), + chrome_options=chrome_options) + return driver + +def main(): + driver = get_chromedriver(use_proxy=True) + #driver.get('https://www.google.com/search?q=my+ip+address') + driver.get('https://httpbin.org/ip') + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..600b6eb --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +selenium==3.141.0