-
Notifications
You must be signed in to change notification settings - Fork 808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
co.set_proxy 无法设置代理 #462
Comments
我的代理IP需要账号密码,利用插件的方式解决的,用起来蛮流畅,希望对你有帮助。网上找到的相关帖子:https://cloud.tencent.com/developer/article/2419799 import string
import os
from DrissionPage import ChromiumOptions, ChromiumPage
# 代理服务器信息(以亿牛云爬虫代理加强版为例)
proxyHost = "www.Host.cn"
proxyPort = "3111"
# 代理认证信息
proxyUser = "username"
proxyPass = "password"
def create_proxy_auth_extension(proxy_host, proxy_port, proxy_username, proxy_password, scheme='http', plugin_path=None):
# 创建Chrome插件的manifest.json文件内容
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "16YUN Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
# 创建Chrome插件的background.js文件内容
background_js = string.Template(
"""
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "${scheme}",
host: "${host}",
port: parseInt(${port})
},
bypassList: ["localhost"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "${username}",
password: "${password}"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
"""
).substitute(
host=proxy_host,
port=proxy_port,
username=proxy_username,
password=proxy_password,
scheme=scheme,
)
# 创建插件目录并写入manifest.json和background.js文件
os.makedirs(plugin_path, exist_ok=True)
with open(os.path.join(plugin_path, "manifest.json"), "w+") as f:
f.write(manifest_json)
with open(os.path.join(plugin_path, "background.js"), "w+") as f:
f.write(background_js)
return os.path.join(plugin_path)
# 指定插件路径
proxy_auth_plugin_path = create_proxy_auth_extension(
plugin_path="/tmp/111",
proxy_host=proxyHost,
proxy_port=proxyPort,
proxy_username=proxyUser,
proxy_password=proxyPass
)
# 使用DrissionPage进行网页自动化,并加载代理认证插件
co = ChromiumOptions().add_extension(path=proxy_auth_plugin_path)
# 禁用扩展
# co.set_argument('--disable-extensions')
page = ChromiumPage(co)
page.get('https://news.163.com/')
# 打印页面标题
print(page.title) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
co.set_proxy('http://119.102.1xx.xx:13880') 太难受了,代理ip不需要账号密码
The text was updated successfully, but these errors were encountered: