API案例

1. 蜂巢指纹浏览器NestBrowser Local API V1

1.1. 功能概述

蜂巢指纹浏览器NestBrowser支持本地Local API的功能,帮助用户通过程序化的方式来读写账号配置信息、启动和关闭浏览器、查询账号等基础API功能,还可以支持第三方Playwright、Selenium、Puppeteer等自动化框架来实现浏览器操作的自动化,拥有高级API权限的用户还可以使用更多功能接口以及定制化的功能。

1.2. API调用案例

Puppeteer调用蜂巢指纹浏览器NestBrowser API案例[语言:javascript]

//javascript
const axios = require('axios');
const puppeteer = require('puppeteer-core');

const ids = '1';    //根据用户自身环境ID修改
const startUrl = 'http://local.nestbrowser.com:43820/api/v1/browser/start?ids=' + ids;

axios.get(startUrl).then(async (res) => {
  console.log(res.data,res.data.code);
  if(res.data.code === 200 && res.data.data && res.data.data.length > 0) {
    try{
        for(let i = 0; i < res.data.data.length; i++){
            const item = res.data.data[i];
            const ws = item.ws;
            console.log('ws:',ws);
            const browser = await puppeteer.connect({browserWSEndpoint: ws.url, defaultViewport:null});
            const page = await browser.newPage();
            await page.goto('https://www.nestbrowser.com/');
            await page.screenshot({ path: `./nestbrowser-${item.id}.png` });
            await browser.close();
            console.log('截图成功;接口工作正常');
        }
    } catch(err){
        console.log(err.message);
    }
  }else{
    console.error('解析参数出错');
  }
}).catch((err) => {
  console.log('接口请求出错,请检查nestbrowser服务是否正常启动')
    console.log('axios catch err:',err.message)
})

Playwright调用蜂巢指纹浏览器NestBrowser API案例[语言:javascript]

//javascript
const axios = require('axios');
const { chromium } = require('playwright');

const ids = '1';    //根据用户自身环境ID修改
const startUrl = 'http://local.nestbrowser.com:43820/api/v1/browser/start?ids=' + ids;

axios.get(startUrl).then(async (res) => {
  console.log(res.data,res.data.code);
  if(res.data.code === 200 && res.data.data && res.data.data.length > 0) {
    try{
        for(let i = 0; i < res.data.data.length; i++){
            const item = res.data.data[i];
            const ws = item.ws;
            console.log('ws:',ws);
            const browser = await chromium.connectOverCDP('http://'+ws.selenium);
            const context = browser.contexts()[0];
            const page = await context.newPage();
            await page.goto('https://www.nestbrowser.com/');
            await page.screenshot({ path: `./nestbrowser-${item.id}.png` });
            await browser.close();
            console.log('截图成功;接口工作正常');
        }
    } catch(err){
        console.log(err.message);
    }
  }else{
    console.error('解析参数出错');
  }
}).catch((err) => {
  console.log('接口请求出错,请检查nestbrowser服务是否正常启动')
    console.log('axios catch err:',err.message)
})

Selenium调用蜂巢指纹浏览器NestBrowser API案例[语言:python]

#python
import requests,time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import sys

ids = '1'   #根据用户自身环境ID修改
open_url = 'http://local.nestbrowser.com:43820/api/v1/browser/start?ids=' + ids #start接口地址
close_url = 'http://local.nestbrowser.com:43820/api/v1/browser/stop?ids=' + ids #close接口地址

resp = requests.get(open_url).json()
print(resp)
if (resp["code"] != 200 or resp["data"][0]["success"] != 'ok'):
    print(resp["data"][0]["success"])
    print("接口请求出错,请检查nestbrowser服务是否正常启动")
    sys.exit()

try:
    item = resp["data"][0]
    chrome_driver = item["webdriver"]
    chrome_options = Options()
    chrome_options.add_experimental_option("debuggerAddress", item["ws"]["selenium"])
    driver = webdriver.Chrome(chrome_driver, options=chrome_options)
    print(driver.title)
    driver.get("https://www.nestbrowser.com")
    time.sleep(1)
    driver.save_screenshot('./selenium-nestbrowser-' + str(ids) + '.png')
    time.sleep(2)
    driver.quit()
    requests.get(close_url)
    print('截图成功;接口工作正常')
except webdriver.WebDriverException as e:
    print('exception: ' + e)
© 2019 all right reserved,powered by Gitbook本文档更新于: 2023-06-27 17:03

results matching ""

    No results matching ""