注意使用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
2
3
docker run -e "MAX_CONCURRENT_SESSIONS=10" -e "ENABLE_DEBUGGER=false" -p 8080:3000 --restart always -d --name browserless browserless/chrome:1-chrome-stable

docker run -e HTTP_PROXY="" -e HTTPS_PROXY="" -p 8888:3000 --restart always -d --name browserless_with_proy browserless/chrome

使用代理 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
2
self.browser = await pyppeteer.connect({"browserWSEndpoint": "ws://10.250.17.39:8080/?stealth"})
self.page = await self.browser.newPage()

自己构造镜像(防墙)

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests
import json

url = "http://localhost:3000/content"

payload = json.dumps({
"url": "https://compassbrand.jinritemai.com/login"
})
headers = {
'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
  • /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
    39
    import 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)