You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.4 KiB
102 lines
2.4 KiB
#!coding: utf-8 |
|
|
|
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 = '112213' # password |
|
|
|
manifest_json = """ |
|
{ |
|
"version": "1.0.0", |
|
"manifest_version": 2, |
|
"name": "Chrome Proxy", |
|
"permissions": [ |
|
"proxy", |
|
"tabs", |
|
"unlimitedStorage", |
|
"storage", |
|
"<all_urls>", |
|
"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: ["<all_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() |
|
|
|
# 最大化窗口启动 |
|
# chrome_options.add_argument("--start-maximized") |
|
|
|
# 如报错 chrome-extensions |
|
# chrome_options.add_argument("--disable-extensions") |
|
|
|
# 防止被反爬 关闭 webdriver flag |
|
# chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) |
|
|
|
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'), |
|
options=chrome_options) |
|
return driver |
|
|
|
|
|
def main(): |
|
import time |
|
driver = get_chromedriver(use_proxy=True) |
|
driver.implicitly_wait(10) # seconds |
|
driver.get('https://httpbin.org/ip') |
|
time.sleep(10) |
|
|
|
if __name__ == '__main__': |
|
main()
|
|
|