查了好多文档大多数都是关于python的,自己终于根据文档找到了 node,js的写法,废话不多说直接上代码,官网链接Browser | Playwright
const { chromium } = require('playwright');
const axios = require('axios');
let headers
(async () => {
const browser = await chromium.connectOverCDP("http://localhost:9001");
const context = await browser.contexts()[0];
const page = await context.pages()[0];
await page.goto('https://www.baidu.com');
// page.on('request', request => console.log('>>', request.method(), request.url()));
// 拦截所有请求
await page.route('**/*', async route => {
headers = route.request().headers();
// console.log('>>', headers.cookie)
// delete headers['X-Secret'];
// await route.continue({ headers });
});
await context.close();
await browser.close();
})();
亲测可用!
还可以打开自己本地的谷歌浏览器
const { chromium } = require('playwright');
const axios = require('axios');
let headers
(async () => {
const browser = await chromium.launch({
headless: false,
// 你本地谷歌浏览器的路径
executablePath: "C:/Program Files/Google/Chrome/Application/chrome.exe"
});
// const browser = await chromium.connectOverCDP("http://localhost:9001");
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://www.bitget.fit/zh-CN/futures/susdt/SBTCSUSDT');
// 拦截所有请求
await page.route('**/*', async route => {
headers = route.request().headers();
// console.log('>>', headers.cookie)
// delete headers['X-Secret'];
// await route.continue({ headers });
});
await context.close();
await browser.close();
})();
指定代理IP打开浏览器
// 启动Playwright并指定代理
const SERVE_PORT = 7157;
const browser = await chromium.launch({
args: ['--proxy-server=' + PROXYURL, "--lang=zh_CN", "--start-maximized"],
headless: false,
timeout: 0,
});
// const browser = await chromium.launch({ headless: false, slowMo: 50 });//模拟打开浏览器,设置有头模式,并通过slowMo属性减慢浏览器的每一步操作
const context = await browser.newContext();//建立context
const page = await context.newPage();//模拟打开一个浏览器的标签页
await page.goto('https://www.bitget.fit/zh-CN/login');//模拟访问网站url