浏览器虚拟化Browserless入门指南
注意使用V1版本
文档V1:https://docs.browserless.io/v1
Docker hub地址:https://hub.docker.com/u/browserless
Github: https://github.com/browserless/chrome
Docker命令
1 | docker pull browserless/chrome:1-chrome-stable |
1 | docker run -e "MAX_CONCURRENT_SESSIONS=10" -e "ENABLE_DEBUGGER=false" -p 8080:3000 --restart always -d --name browserless browserless/chrome:1-chrome-stable |
使用代理 https://docs.browserless.io/v1/docker/config#using-a-proxy
Playwright
1 | browser = pw.chromium.connect_over_cdp(endpoint_url="ws://10.250.17.39:8080/?stealth") |
Pyppeteer
1 | self.browser = await pyppeteer.connect({"browserWSEndpoint": "ws://10.250.17.39:8080/?stealth"}) |
自己构造镜像(防墙)
1 | FROM registry.cn-hangzhou.aliyuncs.com/dockerfork/browserless/chrome:1-chrome-stable |
1 | FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/browserless/chrome:1-chrome-stable |
接口类型
- /content
1 | import requests |
/function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39import requests
brower_fn_api = f"http://localhost:3000/function"
def browserless_login():
"""
企业登录
"""
payload = {
"code": """
module.exports = async ({page, context }) => {
await page.setViewport(viewport = {
'width': 1920,
'height': 1080
});
const {
url,
username,
password
} = context;
await page.evaluateOnNewDocument(()=>{Object.defineProperties(navigator,{webdriver:{get:()=>false}})});
await page.goto(url);
shot = await page.screenshot();
return {
"data": shot,
"type": "image/png",
};
};
""",
"context": {
"url": login_api,
"username": user_info["name"],
"password": user_info["password"],
},
}
res = requests.post(brower_fn_api, json=payload)
with open("tsa_login_shot.png", "wb") as f:
f.write(res.content)
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Chen's Blog!
评论