Playwright是一个强大的Python库,仅用一个API即可自动执行Chromium、Firefox、WebKit等主流浏览器自动化操作,并同时支持以无头模式、有头模式运行。
项目地址:https://playwright.dev/docs/intro
我最喜欢的特点
playwright自定义浏览器设备、时区、经纬度、userAgent、注入脚本
基于nodejs
const { chromium , devices } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false });
//设置设备
const pixel2 = devices['iPhone 6'];
//设置指纹
const context = await browser.newContext({
...pixel2,
//语言
locale: 'de-DE',
//时区
timezoneId: 'America/Adak',
//经纬度
longitude: 29.979097,
latitude: 31.134256,
colorScheme: 'dark',
//设置useragent
userAgent: 'My user agent',
//屏幕大小
viewport: { width: 1440, height: 768 }
});
const page = await browser.newPage();
await page.goto('https://www.baidu.com');
//注入本地脚本
await page.evaluate(() => {
var body = document.getElementsByTagName("body");
var iframe = document.createElement('iframe');
iframe.src="https://blog.csdn.net/huangmingleiluo";
iframe.style.cssText = 'top :300px; position:absolute;';
iframe.width = 1440;
iframe.height = 300;
document.body.appendChild(iframe);
});
// await browser.close();
})();