连接这一行是唯一与 Gologin 相关的部分,其余都是普通的 Playwright 或 Puppeteer 代码。下方每个标签页中的代码都已在 Gologin Cloud Browser 上按原样运行过。
使用 Google 注册,然后从控制面板复制您的密钥。免费,无需信用卡。
在控制面板中点击一下,或调用一次 API。配置文件会保存设备、IP 和登录状态。
安装库,然后用您的令牌和配置文件 ID 运行下面的脚本。要迁移您自己的脚本?把其中的 launch() 调用换成这一行连接代码即可。更多内容请参阅入门指南。
npm install playwright-coreGOLOGIN_TOKEN=your_token GOLOGIN_PROFILE_ID=your_profile_id node scrape.mjswss://cloudbrowser.gologin.com/connect?token=…&profile=…import { chromium } from "playwright-core";
const token = process.env.GOLOGIN_TOKEN;
const profileId = process.env.GOLOGIN_PROFILE_ID;
const endpoint = `wss://cloudbrowser.gologin.com/connect?token=${token}&profile=${profileId}`;
const browser = await chromium.connectOverCDP(endpoint);
const page = await browser.contexts()[0].newPage();
await page.goto("http://quotes.toscrape.com/scroll");
await page.waitForSelector(".quote");
for (let i = 0; i < 30; i++) {
const before = await page.locator(".quote").count();
await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));
try {
await page.waitForFunction(
(n) => document.querySelectorAll(".quote").length > n,
before,
{ timeout: 3000 }
);
} catch {
break;
}
}
const quotes = await page.$$eval(".quote", (els) =>
els.map((el) => ({
text: el.querySelector(".text")?.textContent,
author: el.querySelector(".author")?.textContent,
}))
);
console.log(quotes.length, quotes[0]);
await page.close();
await browser.close();npm install puppeteer-coreGOLOGIN_TOKEN=your_token GOLOGIN_PROFILE_ID=your_profile_id node scrape.mjswss://cloudbrowser.gologin.com/connect?token=…&profile=…import puppeteer from "puppeteer-core";
const token = process.env.GOLOGIN_TOKEN;
const profileId = process.env.GOLOGIN_PROFILE_ID;
const endpoint = `wss://cloudbrowser.gologin.com/connect?token=${token}&profile=${profileId}`;
const browser = await puppeteer.connect({ browserWSEndpoint: endpoint });
const page = await browser.newPage();
await page.goto("http://quotes.toscrape.com/scroll");
await page.waitForSelector(".quote");
for (let i = 0; i < 30; i++) {
const before = await page.$$eval(".quote", (els) => els.length);
await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));
try {
await page.waitForFunction(
(n) => document.querySelectorAll(".quote").length > n,
{ timeout: 3000 },
before
);
} catch {
break;
}
}
const quotes = await page.$$eval(".quote", (els) =>
els.map((el) => ({
text: el.querySelector(".text")?.textContent,
author: el.querySelector(".author")?.textContent,
}))
);
console.log(quotes.length, quotes[0]);
await page.close();
await browser.disconnect();pip install playwrightGOLOGIN_TOKEN=your_token GOLOGIN_PROFILE_ID=your_profile_id python scrape.pywss://cloudbrowser.gologin.com/connect?token=…&profile=…import os
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError
token = os.environ["GOLOGIN_TOKEN"]
profile_id = os.environ["GOLOGIN_PROFILE_ID"]
endpoint = f"wss://cloudbrowser.gologin.com/connect?token={token}&profile={profile_id}"
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(endpoint)
page = browser.contexts[0].new_page()
page.goto("http://quotes.toscrape.com/scroll")
page.wait_for_selector(".quote")
for _ in range(30):
before = page.locator(".quote").count()
page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
try:
page.wait_for_function(
"n => document.querySelectorAll('.quote').length > n",
arg=before,
timeout=3000,
)
except PlaywrightTimeoutError:
break
quotes = page.eval_on_selector_all(
".quote",
"els => els.map(el => ({text: el.querySelector('.text')?.textContent, author: el.querySelector('.author')?.textContent}))",
)
print(len(quotes), quotes[0])
page.close()
browser.close()npm install playwrightGOLOGIN_TOKEN=your_token GOLOGIN_PROFILE_ID=your_profile_id npx playwright testwss://cloudbrowser.gologin.com/connect?token=…&profile=…import { chromium, expect, test as base } from "playwright/test";
const endpoint = `wss://cloudbrowser.gologin.com/connect?token=${process.env.GOLOGIN_TOKEN}&profile=${process.env.GOLOGIN_PROFILE_ID}`;
const test = base.extend({
browser: [
async ({}, use) => {
const browser = await chromium.connectOverCDP(endpoint);
await use(browser);
await browser.close();
},
{ scope: "worker" },
],
});
test("quotes load in Gologin Cloud Browser", async ({ page }) => {
await page.goto("http://quotes.toscrape.com/");
await expect(page.locator(".quote").first()).toBeVisible();
});
test("the next page loads too", async ({ page }) => {
await page.goto("http://quotes.toscrape.com/page/2/");
await expect(page.locator(".quote")).toHaveCount(10);
});选择您的团队已经熟悉的库。两者使用的云浏览器和连接地址都相同。
由 Microsoft 推出,支持 Node.js、Python、Java 和 .NET。会自动等待元素出现,并自带测试运行器。在 Gologin 上:安装 playwright-core,并用 chromium.connectOverCDP 连接。
由 Chrome 团队推出,支持 Node.js。一个更小巧的库,直接使用 Chrome DevTools Protocol 通信。在 Gologin 上:安装 puppeteer-core,并用 puppeteer.connect 连接。
无论脚本现在在哪里运行,迁移的关键都是这一行连接代码。下面是您还可以去掉的部分。
去掉浏览器镜像。安装不带浏览器的库,然后连接。
部署包中无需 Chromium 层。函数只需打开一个 WebSocket。
添加一个 fixture,参见 Playwright Test 标签页。测试保持原样。
把地址换成 Gologin 的连接代码。调用一次 API 即可添加代理。
每个套餐运行的都是同一款浏览器,并包含住宅 IP。套餐的区别在于浏览器小时数和并行会话数。
$0
无需信用卡
$4.50/月
适合个人开发者
$8/月
适合成长中的团队
$14/月
适合关键业务
需要同时运行更多?任何套餐都可以加购并行会话,每个每月 $2,按需添加。超出额度的代理流量为 $1/GB。
4 个并行会话起,配备专属客户经理、高级 SLA 和定制化上手指导。
一个账户,一个代理网络,一套生态。按任务需要选择产品。
两者都能用同一行连接代码在 Gologin Cloud Browser 上运行,所以选择您的团队熟悉的那个即可。Playwright 支持的语言更多,并自带测试运行器;Puppeteer 是 Chrome 团队推出的、更小巧的 Node.js 库。在您自己的电脑上,Playwright 还能驱动 Firefox 和 WebKit;在云端,两者驱动的都是同一款基于 Chromium 的浏览器。
Playwright 使用 chromium.connectOverCDP,Python Playwright 使用 connect_over_cdp,Puppeteer 使用 puppeteer.connect 搭配 browserWSEndpoint。三者都指向 wss://cloudbrowser.gologin.com/connect,并带上您的令牌和配置文件 ID。脚本的其余部分保持不变。要从滚动时才加载的页面上采集数据?请参阅动态网页抓取。
不需要。安装不带浏览器的 playwright-core 或 puppeteer-core,然后连接到云端。无需锁定 Chrome 版本,无需重新构建 Docker 镜像,您的服务器上也没有任何浏览器依赖。请参阅 无头浏览器 API。
能。登录状态保存在配置文件中。用同一个配置文件 ID 连接,并在配置文件自己的窗口中打开页面,也就是 Playwright 中的 browser.contexts()[0],就像上面的脚本那样:下次运行开始时就已登录,并使用同一个住宅 IP。用 browser.newPage() 打开的页面会在一个新窗口中打开,不带这个登录状态。需要手动完成 2FA 步骤?请参阅 Cloud Browser UI。
能。添加一个用 connectOverCDP 连接浏览器的 fixture,参见上面的 Playwright Test 标签页,每个测试就都会在 Gologin Cloud Browser 中运行。测试及其断言保持原样,npx playwright test 也照常运行它们。
免费版和 Professional 为 1 个,Business 为 2 个,Enterprise 为 3 个,Custom 为 4 个起。任何套餐都可以加购更多,每个每月 $2。每个并行会话就是一个已连接的浏览器。