python+playwright 学习-85 启动参数 proxy 设置代理几种方式

2023-11-08 18:21:08 浏览数 (2)

前言

在使用playwright执行代码的时候,如需设置代理,可以在启动的时候加proxy 参数设置代理。 本篇总结下可以加proxy代理的几种方式。

launch 启动全局代理

launch 启动的时候设置全局代理,以下是示例

代码语言:javascript复制
from playwright.sync_api import sync_playwrightwith sync_playwright() as playwright:
    browser = playwright.chromium.launch(
        headless=False,         proxy={"server": "http://myproxy.com:3128"}
    )
    context = browser.new_context()

    page = context.new_page()
    page.goto('http://47.108.155.10/login.html')

    browser.close()

proxy 参数说明

proxy 传 dict 类型,可以设置的参数有

  • server 例如,支持HTTP和SOCKS代理http://myproxy.com:3128或socks5://myproxy.com:3128。 缩写myproxy.com:3128被认为是HTTP代理
  • bypass 可选,逗号分隔域以绕过代理,例如“.com、chromium.org、.domain.com”。
  • username 可选,如果HTTP代理需要身份验证,则使用用户名。
  • password 可选,如果HTTP代理需要身份验证,则使用密码。

带账号密码的代理示例

代码语言:javascript复制
from playwright.sync_api import sync_playwrightwith sync_playwright() as playwright:
    browser = playwright.chromium.launch(
        headless=False,        proxy={                       "server": "http://myproxy.com:3128",                       'username': 'p-username',  # 需要身份验证,可以通过这里指定用户名和密码
            'password': 'p-password'
        }
    )

对于Windows上的Chromium,浏览器需要使用全局代理才能运行此选项。如果所有上下文都覆盖代理,则全局代理将永远不会使用,并且可以是任何字符串,例如launch({proxy:{server:’http://per-context“}}”)。

context上下文设置代理

对于Windows上的Chromium,浏览器需要launch设置全局代理才能运行此选项。 如果上下文也设置了proxy代理,会覆盖launch设置全局代理。

代码语言:javascript复制
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
    browser = playwright.chromium.launch(
        headless=False,         proxy={                     "server": "http://myproxy.com:3128",                     'username': 'p-username',  # 需要身份验证,可以通过这里指定用户名和密码
         'password': 'p-password'
        }
    )
    context = browser.new_context(    proxy={'server': 'http://127.0.0.1:8888'}
)
    page = context.new_page()
    page.goto('http://47.108.155.10/login.html')

对于firefox 和 webkit 系列浏览器,可以不用在launch设置proxy

代码语言:javascript复制
from playwright.sync_api import sync_playwrightwith sync_playwright() as playwright:
    browser = playwright.firefox.launch(headless=False)
    context = browser.new_context(proxy={      'server': 'http://127.0.0.1:8888'
    })
    page = context.new_page()
    page.goto('http://47.108.155.10/login.html')

new_page() 设置代理

基于 context 创建的的new_page() 没有proxy参数设置

代码语言:javascript复制
    context = browser.new_context()
    page = context.new_page()

基于browser 创建的new_page() 有proxy参数设置

代码语言:javascript复制
from playwright.sync_api import sync_playwrightwith sync_playwright() as playwright:
    browser = playwright.firefox.launch(headless=False)
    page = browser.new_page(         proxy={'server': 'http://127.0.0.1:8888'}    )
    page.goto('http://47.108.155.10/login.html')

非无痕模式启动浏览器launch_persistent_context

非无痕模式启动浏览器只能在launch_persistent_context()添加proxy参数

代码语言:javascript复制
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
    context = playwright.chromium.launch_persistent_context(        # 指定本机用户缓存地址
        user_data_dir=f"D:chrome_userxyoyo",
        headless=False,
        proxy={                         'server': 'http://127.0.0.1:8888'
        }
    )
    page = context.pages[0]
    page.goto('http://47.108.155.10/login.html')

0 人点赞