自动化
Puppeteer 与 Playwright,直接连到你的配置文件
每个打开的配置文件都有自己的调试地址。把你的代码连到那个配置文件上:它的指纹、它的 Cookie、它的代理都在。所有套餐都包含,免费套餐也一样。
- 1在程序里打开本地接口:设置中的「本地 API」,复制密钥。
- 2请求打开一个配置文件。返回的地址只属于这个配置文件。
- 3像连接普通浏览器一样,把你的代码连到这个地址。
- 4照常工作。指纹、Cookie 和代理都已经就位。
JavaScript (puppeteer-core)
const puppeteer = require('puppeteer-core');
(async () => {
// 1. Ask the app for this profile's debugging endpoint.
const started = await fetch(
'http://127.0.0.1:50325/api/v1/browser/start?profileId=YOUR_PROFILE_ID',
{ method: 'POST', headers: { 'x-api-token': 'YOUR_API_TOKEN' } },
).then((r) => r.json());
// 2. Attach to it like to any other browser.
const browser = await puppeteer.connect({
browserWSEndpoint: started.data.ws,
defaultViewport: null,
});
const [page] = await browser.pages();
await page.goto('https://example.com');
})();
Python (Playwright)
import requests
from playwright.sync_api import sync_playwright
# 1. Ask the app for this profile's debugging endpoint.
started = requests.post(
"http://127.0.0.1:50325/api/v1/browser/start",
params={"profileId": "YOUR_PROFILE_ID"},
headers={"x-api-token": "YOUR_API_TOKEN"},
).json()
# 2. Attach to it like to any other browser.
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(started["data"]["ws"])
page = browser.contexts[0].pages[0]
page.goto("https://example.com")
连接之前需要知道的一件事
Puppeteer 和 Playwright 一连上就会自己打开 Runtime.enable 和 Page.enable。这两个域会在 Chromium 内部装上观察器,而页面脚本能够察觉。我们自己的脚本编排器从不打开它们,所以由它驱动的配置文件与手动操作的一模一样。如果你要打交道的网站会检查这一点,就在程序里编排步骤。