Nstbrowser是一款专注于隐私保护和指纹管理的浏览器,使其适用于需要定制指纹环境的开发者或业务场景。
本教程旨在帮助开发者快速上手安装和使用Nstbrowser Agent。它涵盖了从脚本安装到通过Puppeteer操作浏览器的整个过程。阅读本文后,您将能够:
为了确保服务顺利运行,在开始设置之前,请确保您的服务器满足以下要求:
Nstbrowser GitHub提供了一个方便的安装脚本。您可以使用以下命令直接下载并运行它:
wget -qO- https://raw.githubusercontent.com/Nstbrowser/nstbrowser-agent-setup/refs/heads/main/scripts/agent_install.sh | sudo bash
注意: 默认情况下,安装脚本将下载并安装必要的字体和内核。如果您不需要这些组件,可以使用以下选项跳过它们:
--no-kernel
--no-fonts
例如,如果您只想安装Nstbrowser-agent而不安装字体和内核,请运行以下命令:
wget -qO- https://raw.githubusercontent.com/Nstbrowser/nstbrowser-agent-setup/refs/heads/main/scripts/agent_install.sh | sudo bash -s --no-kernel --no-fonts
安装过程分解
安装脚本将自动完成以下任务:
安装完成后,您将看到提示:“agent install success!”
安装完成后,运行以下命令启动Nstbrowser-agent:
sudo ./agent --referer=client
默认情况下,Nstbrowser-agent使用端口8848提供服务。启动后,您可以通过访问http://<YOUR_SERVER_IP>:8848
来访问您的爬虫服务器。
安装并启动Nstbrowser-agent后,您可以开始使用它进行爬虫开发。由于Nstbrowser支持Puppeteer和Playwright等主流自动化工具,您可以轻松地将其集成到您现有的爬虫框架中。
例如,如果您使用Puppeteer,您可以使用以下代码连接到Nstbrowser-agent:
import puppeteer from 'puppeteer-core';
async function execPuppeteer(browserWSEndpoint) {
try {
const browser = await puppeteer.connect({
browserWSEndpoint: browserWSEndpoint,
});
const page = await browser.newPage();
await page.goto('https://www.google.com');
await page.screenshot({ fullPage: true, path: "google.png" })
await browser.close()
} catch (err) {
console.error(err);
}
}
async function createAndConnectToBrowser() {
const host = 'YOU_SERVER_IP:8848';
const apiKey = 'YOU_API_KEY';
const config = {
headless: true, // Must be true
name: 'NstProfile',
platform: 'linux',
kernel: 'chromium',
kernelMilestone: '132',
once: true,
};
const query = new URLSearchParams({
'x-api-key': apiKey, // required
config: encodeURIComponent(JSON.stringify((config))),
});
const browserWSEndpoint = `ws://${host}/devtool/launch?${query.toString()}`;
await execPuppeteer(browserWSEndpoint)
}
createAndConnectToBrowser().then();
注意: 由于Nstbrowser-agent通常在没有虚拟图形显示的无头服务器环境中运行,因此浏览器只能在无头模式下启动。
Nstbrowser-agent提供强大的API支持,允许用户以编程方式创建和管理浏览器配置。这些配置可用于自动化测试、爬虫开发或其他需要浏览器实例的场景。
接下来,我们演示一个具体的API使用示例:Createfile
import axios from 'axios';
async function createProfile() {
const host = 'YOU_SERVER_IP:8848';
const apiUrl = `http://${host}/api/agent/profile`;
const apiKey = 'YOU_API_KEY';
const profileData = {
name: 'NstProfile',
platform: 'linux',
kernel: 'chromium',
kernelMilestone: '132',
proxy: 'http://username:password@proxy_ip:proxy_port',
startupUrls: ['https://www.google.com'], // The URL that is loaded at startup
args: {
"--headless": true // chrome startup parameter
}
};
try {
const response = await axios.post(apiUrl, profileData, {
headers: {
'x-api-key': apiKey,
'Content-Type': 'application/json',
},
});
console.log('Profile created successfully:', response.data);
} catch (error) {
console.error('Failed to create profile:', error);
}
}
createProfile();
运行成功后,您将得到以下结果:
Profile created successfully: {
data: {
profileId: 'YOU_PROFILEID',
fingerprintId: 'YOU_FINGERPRINTID',
groupId: 'YOU_GROUP_ID',
teamId: 'YOU_TEAM_ID',
userId: 'YOU_USER_ID',
name: 'NstProfile',
kernel: 0,
kernelVersion: '',
kernelMilestone: '132',
uaFullVersion: '132.0.6834.83',
platform: 2,
platformVersion: '6.3.3',
saveLocal: false,
status: 1,
note: '',
tags: null,
_id: 'YOU_ID',
createdAt: '2025-01-01T00:00:00.000Z',
updatedAt: '2025-01-01T00:00:00.000Z'
},
err: false,
msg: 'success',
code: 200
}
以下是Nstbrowser的官方资源链接。开发者可以根据需要进一步探索: