from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests
def seleniumSetUP(ip,port):
# 设置Chrome驱动程序的路径
# 创建Chrome浏览器实例
chrome_options = Options()
# 配置获取到的ip和port
chrome_options.add_argument(f'--proxy-server=http://{ip}:{port}')
browser = webdriver.Chrome(options=chrome_options)
# 使用代理访问
browser.get('http://ipinfo.io')
print(browser.page_source)
if __name__ == '__main__':
# 获取代理的url,一次仅获取一条
porxyUrl = "http://api.proxy.ipidea.io/getProxyIp?num=1&return_type=json&lb=1&sb=0&flow=1®ions=&protocol=http"
# 访问并获取代理
ipInfo = requests.get(porxyUrl)
print(ipInfo.json())
info = ipInfo.json()["data"]
# 解析json,获取代理服务器地址
ip = info[0]["ip"]
# 解析json,获取代理的端口
port = info[0]["port"]
# 获取到的代理信息传入到selenium中进行配置
seleniumSetUP(ip,port)
from selenium import webdriver
import zipfile
#selenium账密代理需要自行打包selenium插件才能挂代理
# 打包Google代理插件
def create_proxyauth_extension(proxy_host, proxy_port, proxy_username, proxy_password, scheme='http', plugin_path=None):
if plugin_path is None:
# 插件地址
plugin_path = './vimm_chrome_proxyauth_plugin.zip'
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 = f"""
var config = {{
mode: "fixed_servers",
rules: {{
singleProxy: {{
scheme: "{scheme}",
host: "{proxy_host}",
port: parseInt({proxy_port})
}},
bypassList: ["foobar.com"]
}}
}};
chrome.proxy.settings.set({{value: config, scope: "regular"}}, function() {{}});
function callbackFn(details) {{
return {{
authCredentials: {{
username: "{proxy_username}",
password: "{proxy_password}"
}}
}};
}}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{{urls: ["<all_urls>"]}},
['blocking']
);
"""
with zipfile.ZipFile(plugin_path, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return plugin_path
# 填写主机地址,端口,账号,密码
# 自定义测试代理,可自己修改
proxyauth_plugin_path = create_proxyauth_extension(
proxy_host = "proxy.ipidea.io",
proxy_port = 2333,
proxy_username = "账户",
proxy_password = "密码"
)
# 测试
co = webdriver.ChromeOptions()
co.add_extension('./vimm_chrome_proxyauth_plugin.zip')
# 无头浏览器开启
# co.add_argument("--headless=chrome")
driver = webdriver.Chrome(options=co)
driver.get('http://ipinfo.io/')
print(driver.page_source)
driver.close()