在 DrissionPage 中设置代理

DrissionPage 基于 Chromium,可以通过 ChromiumOptions 来设置代理。以下是几种设置代理的方法:

方法一:基础代理设置(HTTP/HTTPS)

python 复制代码
from DrissionPage import Chromium
from DrissionPage.common import ChromiumOptions

# 创建 ChromiumOptions 对象
co = ChromiumOptions()

# 设置代理服务器地址和端口
co.set_proxy('http://127.0.0.1:1080')  # 替换为你的代理地址

# 创建浏览器实例并传入选项
browser = Chromium(co)

# 打开页面测试
browser.get('https://httpbin.org/ip')
print(browser.html)

方法二:带认证的代理设置

python 复制代码
from DrissionPage import Chromium
from DrissionPage.common import ChromiumOptions

co = ChromiumOptions()

# 设置带用户名和密码的代理
# 格式: http://username:password@proxy_host:proxy_port
proxy_url = 'http://username:password@127.0.0.1:1080'
co.set_proxy(proxy_url)

browser = Chromium(co)
browser.get('https://httpbin.org/ip')

方法三:SOCKS 代理设置

python 复制代码
from DrissionPage import Chromium
from DrissionPage.common import ChromiumOptions

co = ChromiumOptions()

# SOCKS5 代理
co.set_proxy('socks5://127.0.0.1:1080')

# SOCKS4 代理
# co.set_proxy('socks4://127.0.0.1:1080')

browser = Chromium(co)
browser.get('https://httpbin.org/ip')

方法四:使用代理列表随机选择

python 复制代码
from DrissionPage import Chromium
from DrissionPage.common import ChromiumOptions
import random

# 代理列表
proxies = [
    'http://127.0.0.1:1080',
    'http://127.0.0.1:1081',
    'http://127.0.0.1:1082',
    'socks5://127.0.0.1:1083'
]

# 随机选择一个代理
proxy = random.choice(proxies)

co = ChromiumOptions()
co.set_proxy(proxy)

browser = Chromium(co)
browser.get('https://httpbin.org/ip')

方法五:验证代理是否生效

python 复制代码
from DrissionPage import Chromium
from DrissionPage.common import ChromiumOptions

co = ChromiumOptions()
co.set_proxy('http://127.0.0.1:1080')  # 替换为你的代理

# 可选:设置无头模式
# co.headless = True

browser = Chromium(co)

# 访问 IP 检测网站
browser.get('https://httpbin.org/ip')

# 打印返回的 IP 地址
print("当前 IP:", browser.ele('body').text)

# 或者获取完整响应
print("完整响应:", browser.html)

browser.quit()

完整示例:带错误处理

python 复制代码
from DrissionPage import Chromium
from DrissionPage.common import ChromiumOptions
import time

def create_browser_with_proxy(proxy_url):
    """创建带代理的浏览器实例"""
    try:
        co = ChromiumOptions()
        co.set_proxy(proxy_url)
        
        # 可选配置
        co.headless = False  # 显示浏览器窗口
        co.incognito = True  # 隐身模式
        
        browser = Chromium(co)
        return browser
    except Exception as e:
        print(f"创建浏览器失败: {e}")
        return None

# 使用示例
proxy = 'http://127.0.0.1:1080'  # 替换为你的代理
browser = create_browser_with_proxy(proxy)

if browser:
    try:
        # 测试代理
        browser.get('https://httpbin.org/ip', timeout=30)
        time.sleep(2)
        print("代理连接成功")
        print("当前 IP:", browser.ele('body').text)
        
        # 你的爬虫逻辑...
        
    except Exception as e:
        print(f"访问失败: {e}")
    finally:
        browser.quit()

注意事项

  1. 代理格式

    • HTTP/HTTPS: http://host:porthttp://user:pass@host:port
    • SOCKS5: socks5://host:portsocks5://user:pass@host:port
    • SOCKS4: socks4://host:port
  2. 代理类型:确保你的代理服务器支持相应的协议

  3. 连接超时:可以设置超时参数避免长时间等待

    python 复制代码
    co.set_proxy('http://127.0.0.1:1080', timeout=10)
  4. 代理认证:如果代理需要认证,确保用户名和密码正确编码(特殊字符需要 URL 编码)

  5. 验证代理 :使用 https://httpbin.org/iphttps://ipinfo.io/json 等网站验证代理是否生效

希望这些示例能帮助你在 DrissionPage 中成功设置代理!

相关推荐
睡不醒男孩0308232 小时前
第一篇:多云与多模态时代的企业级数据库云管理平台(DBaaS)选型指南
数据库·clup·中启乘数
小二·2 小时前
向量数据库实战
数据库
炘爚3 小时前
Phase 5:MySQL 连接池
数据库·mysql
j_xxx404_3 小时前
MySQL库操作硬核解析:字符集、校验规则、大小写比较、备份恢复与连接排查
运维·服务器·数据库·人工智能·mysql·ai·oracle
minji...3 小时前
MySQL数据库 (五) MySQL表的约束(上),非空约束,默认值约束,零填充约束,主键约束,符合主键
数据库·mysql·表的约束·主键约束·非空约束·复合主键·零填充约束
拾贰_C4 小时前
【python | installation 】python 安装 | Windows | 命令使用
linux·数据库·ubuntu
贺今宵4 小时前
Vue 3 + Capacitor 使用jeep-sqlite,web端使用本地sqlite数据库
前端·数据库·vue.js·sqlite·web
列星随旋4 小时前
MySQL面经整理
数据库·mysql
AllData公司负责人4 小时前
大模型赋能AllData数据中台,系列升级|通过联合智谱大模型与Chat2DB开源项目,建设Text2SQL生产场景全新体验的数据源平台!
数据库·人工智能·text2sql·数据中台·数据源·chat2db·智谱大模型
minji...5 小时前
MySQL数据库 (四) MySQL的数据类型,tinyint,float,decimal,枚举enum和集合set
数据库·mysql·tinyint·enum·decimal·varchar·bit