python - undetected_chromedriver.v2 代理不工作 (proxy_auth_plugin.zip)
问题描述
我正在为 selenium chrome webdriver 使用 proxy_auth_plugin.zip 代理,但我改用 undetected_chromedriver.v2 它与 selenium chrome webdriver 相同,但它更好地用于安全问题
~
试图自动更改代理但它与我在 selenium chrome 上的工作方式不同
适合我的python selenium chrome代码:
import os
import zipfile
import time
from selenium import webdriver
PROXY_HOST = '' # rotating proxy
PROXY_PORT = 0
PROXY_USER = ''
PROXY_PASS = ''
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()
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, 'C:\cc\chromedriver.exe'),
chrome_options=chrome_options)
return driver
def main():
driver = get_chromedriver(use_proxy=True)
driver.get('')
time(10)
if __name__ == '__main__':
main()
这是我的 undetected_chromedriver.v2 代理代码
import time
import random
from typing import Any, Union
import undetected_chromedriver.v2 as uc
import os
import zipfile
import time
from time import sleep
host = '107.23.19.8'
port = 3135
username = 'ggwp'
password = 'ggwp123'
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']
);
""" % (host, port , username, password)
def get_chromedriver(use_proxy=False, user_agent=None):
# opts = uc.ChromeOptions()
# opts.add_argument(proxySetting.GetProxyText())
#driver = uc.Chrome(options=opts)
path = os.path.dirname(os.path.abspath(__file__))
chrome_options = uc.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 = uc.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://ipleak.net')
time(10)
if __name__ == '__main__':
main()
解决方案
推荐阅读
- sql - SQL - VARCHAR 到日期,其中分隔符是句点而不是破折号 SQL
- reactjs - 从 react redux 使用调度和连接
- javascript - 如何使用 Sidebar material-ui React 更改组件
- javascript - SAPUI5 重新加载页面后恢复应用程序状态
- reactjs - 在formik中给出文件输入时如何防止刷新状态值
- multithreading - 对于在具有静态生命周期的线程之间共享的只读静态变量,使用 Box::leak 还是 Arc 更好?
- python - Librosa 将音频时间序列 [y] 写为 float64,即使指定写为 float32
- kotlin - 使用 Android Studio 运行时,Gradle 测试任务会产生不同的结果
- maven - 我们可以在 Maven 中的 Build 标签之后添加 Modules 标签吗?
- reactjs - 从函数中的json返回数据