Playwright 集成教程
Playwright 是微软开发的现代化浏览器自动化工具,支持多种编程语言。
Python + Playwright
安装
bash
pip install playwright
playwright install1
2
2
基本使用
python
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(proxy={
'server': 'http://proxy.okkproxy.com:8080',
'username': 'your_username',
'password': 'your_password'
})
page = browser.new_page()
page.goto('https://httpbin.org/ip')
print(page.content())
browser.close()1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
异步使用
python
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(proxy={
'server': 'http://proxy.okkproxy.com:8080',
'username': 'your_username',
'password': 'your_password'
})
page = await browser.new_page()
await page.goto('https://example.com')
await browser.close()
asyncio.run(main())1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Node.js + Playwright
安装
bash
npm install playwright1
使用示例
javascript
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({
proxy: {
server: 'http://proxy.okkproxy.com:8080',
username: 'your_username',
password: 'your_password'
}
});
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();
})();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
高级功能
指定地区
python
# 在用户名中指定国家代码
browser = p.chromium.launch(proxy={
'server': 'http://proxy.okkproxy.com:8080',
'username': 'your_username-US', # 美国IP
'password': 'your_password'
})1
2
3
4
5
6
2
3
4
5
6
多个浏览器上下文
python
browser = p.chromium.launch()
# 为每个上下文使用不同的代理
context1 = browser.new_context(proxy={
'server': 'http://proxy1.okkproxy.com:8080',
'username': 'user1',
'password': 'pass1'
})
context2 = browser.new_context(proxy={
'server': 'http://proxy2.okkproxy.com:8080',
'username': 'user2',
'password': 'pass2'
})1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
性能优化
python
# 禁用图片加载提高速度
context = browser.new_context(
proxy={
'server': 'http://proxy.okkproxy.com:8080',
'username': 'your_username',
'password': 'your_password'
}
)
await context.route('**/*.{png,jpg,jpeg,gif,svg,ico}',
lambda route: route.abort())1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11