Nstbrowser Python SDK
用于与Nstbrowser API v2交互的Python SDK
概述
此SDK实现了Nstbrowser API v2,这是推荐的API,它具有v1的完整功能以及其他附加功能。它提供了一套全面的工具,用于管理浏览器配置文件、控制浏览器实例、管理本地浏览器数据以及利用Chrome DevTools Protocol (CDP)进行浏览器自动化。
此SDK使您可以:
- 创建和管理具有详细指纹配置的浏览器配置文件
- 单独或批量启动和停止浏览器实例
- 为配置文件配置和管理代理
- 管理配置文件标签以更好地组织
- 清除浏览器缓存和Cookie
- 使用Chrome DevTools Protocol (CDP)连接到浏览器
- 通过CDP集成自动化浏览器操作
安装
bash
pip install nstbrowser
pip install nstbrowser
开始使用
要使用此SDK,您需要一个来自Nstbrowser的API密钥:
python
from nstbrowser import NstbrowserClient
# 使用您的API密钥初始化客户端
client = NstbrowserClient(api_key="your_api_key")
# 现在您可以使用各种服务
profile_id = "your_profile_id"
# 启动浏览器实例
response = client.browsers.start_browser(profile_id=profile_id)
print(f"Browser started: {response}")
# 停止浏览器实例
response = client.browsers.stop_browser(profile_id=profile_id)
print(f"Browser stopped: {response}")
from nstbrowser import NstbrowserClient
# 使用您的API密钥初始化客户端
client = NstbrowserClient(api_key="your_api_key")
# 现在您可以使用各种服务
profile_id = "your_profile_id"
# 启动浏览器实例
response = client.browsers.start_browser(profile_id=profile_id)
print(f"Browser started: {response}")
# 停止浏览器实例
response = client.browsers.stop_browser(profile_id=profile_id)
print(f"Browser stopped: {response}")
可用服务
此SDK提供以下服务:
BrowsersService
管理浏览器实例,包括启动、停止和获取有关浏览器的信息。
python
# 为特定配置文件启动浏览器
response = client.browsers.start_browser(profile_id="your_profile_id")
# 批量启动多个浏览器
response = client.browsers.start_browsers(profile_ids=["profile_id_1", "profile_id_2"])
# 使用自定义配置启动一次性浏览器
config = {
"name": "testProfile",
"platform": "Windows",
"kernelMilestone": "132",
"headless": False,
"proxy": "http://admin:[email protected]:8000",
"fingerprint": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ...",
"screen": {"width": 1280, "height": 1024}
},
"startupUrls": ["https://www.example.com"]
}
response = client.browsers.start_once_browser(data=config)
# 停止浏览器
response = client.browsers.stop_browser(profile_id="your_profile_id")
# 停止多个浏览器
response = client.browsers.stop_browsers(profile_ids=["profile_id_1", "profile_id_2"])
# 获取活动浏览器
response = client.browsers.get_browsers(status="running")
# 获取浏览器页面
response = client.browsers.get_browser_pages(profile_id="your_profile_id")
# 获取浏览器调试器信息
response = client.browsers.get_browser_debugger(profile_id="your_profile_id")
# 为特定配置文件启动浏览器
response = client.browsers.start_browser(profile_id="your_profile_id")
# 批量启动多个浏览器
response = client.browsers.start_browsers(profile_ids=["profile_id_1", "profile_id_2"])
# 使用自定义配置启动一次性浏览器
config = {
"name": "testProfile",
"platform": "Windows",
"kernelMilestone": "132",
"headless": False,
"proxy": "http://admin:[email protected]:8000",
"fingerprint": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ...",
"screen": {"width": 1280, "height": 1024}
},
"startupUrls": ["https://www.example.com"]
}
response = client.browsers.start_once_browser(data=config)
# 停止浏览器
response = client.browsers.stop_browser(profile_id="your_profile_id")
# 停止多个浏览器
response = client.browsers.stop_browsers(profile_ids=["profile_id_1", "profile_id_2"])
# 获取活动浏览器
response = client.browsers.get_browsers(status="running")
# 获取浏览器页面
response = client.browsers.get_browser_pages(profile_id="your_profile_id")
# 获取浏览器调试器信息
response = client.browsers.get_browser_debugger(profile_id="your_profile_id")
ProfilesService
管理浏览器配置文件,包括创建、删除、代理配置和标签管理。
python
# 创建新的配置文件
profile_data = {
"name": "New Profile",
"platform": "Windows",
"kernelMilestone": "132",
"fingerprint": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ...",
"timezone": "Asia/Hong_Kong"
}
}
response = client.profiles.create_profile(data=profile_data)
# 获取具有过滤功能的配置文件
response = client.profiles.get_profiles(data={"page": 1, "pageSize": 10})
# 删除配置文件
response = client.profiles.delete_profile(profile_id="your_profile_id")
# 删除多个配置文件
response = client.profiles.delete_profiles(profile_ids=["profile_id_1", "profile_id_2"])
# 更新配置文件的代理
response = client.profiles.update_profile_proxy(profile_id="your_profile_id", data={"url": "http://admin:[email protected]:8000"})
# 重置配置文件的代理
response = client.profiles.reset_profile_proxy(profile_id="your_profile_id")
# 管理配置文件标签
response = client.profiles.create_profile_tags(profile_id="your_profile_id", data=[{"name": "social", "color": "#646AEE"}])
response = client.profiles.get_profile_tags()
# 创建新的配置文件
profile_data = {
"name": "New Profile",
"platform": "Windows",
"kernelMilestone": "132",
"fingerprint": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ...",
"timezone": "Asia/Hong_Kong"
}
}
response = client.profiles.create_profile(data=profile_data)
# 获取具有过滤功能的配置文件
response = client.profiles.get_profiles(data={"page": 1, "pageSize": 10})
# 删除配置文件
response = client.profiles.delete_profile(profile_id="your_profile_id")
# 删除多个配置文件
response = client.profiles.delete_profiles(profile_ids=["profile_id_1", "profile_id_2"])
# 更新配置文件的代理
response = client.profiles.update_profile_proxy(profile_id="your_profile_id", data={"url": "http://admin:[email protected]:8000"})
# 重置配置文件的代理
response = client.profiles.reset_profile_proxy(profile_id="your_profile_id")
# 管理配置文件标签
response = client.profiles.create_profile_tags(profile_id="your_profile_id", data=[{"name": "social", "color": "#646AEE"}])
response = client.profiles.get_profile_tags()
LocalsService
管理本地浏览器数据,例如缓存和Cookie。
python
# 清除浏览器缓存
response = client.locals.clear_profile_cache(profile_id="your_profile_id")
# 清除浏览器Cookie
response = client.locals.clear_profile_cookies(profile_id="your_profile_id")
# 清除浏览器缓存
response = client.locals.clear_profile_cache(profile_id="your_profile_id")
# 清除浏览器Cookie
response = client.locals.clear_profile_cookies(profile_id="your_profile_id")
CdpEndpointsService
提供使用Chrome DevTools Protocol (CDP)连接到浏览器以进行自动化的功能。
python
# 使用CDP连接到浏览器
config = {
"headless": False,
"autoClose": False
}
response = client.cdp_endpoints.connect_browser(profile_id="your_profile_id", config=config)
# 使用CDP连接到一次性浏览器
config = {
"name": "testProfile",
"platform": "Windows",
"kernelMilestone": "132",
"autoClose": False,
"headless": False
}
response = client.cdp_endpoints.connect_once_browser(config=config)
# 使用CDP连接到浏览器
config = {
"headless": False,
"autoClose": False
}
response = client.cdp_endpoints.connect_browser(profile_id="your_profile_id", config=config)
# 使用CDP连接到一次性浏览器
config = {
"name": "testProfile",
"platform": "Windows",
"kernelMilestone": "132",
"autoClose": False,
"headless": False
}
response = client.cdp_endpoints.connect_once_browser(config=config)
示例
此SDK在/examples
目录中提供了一套全面的示例,按服务进行组织:
浏览器示例
browsers/start_browser.py
: 为特定配置文件启动浏览器browsers/start_browsers.py
: 批量启动多个浏览器browsers/start_once_browser.py
: 使用自定义配置启动一次性浏览器browsers/stop_browser.py
: 停止浏览器browsers/stop_browsers.py
: 停止多个浏览器browsers/get_browsers.py
: 获取浏览器状态信息browsers/get_browser_pages.py
: 获取浏览器页面信息browsers/get_browser_debugger.py
: 获取浏览器调试器信息
配置文件示例
profiles/create_profile.py
: 创建具有详细配置的新配置文件profiles/get_profiles.py
: 获取具有过滤选项的配置文件profiles/delete_profile.py
: 删除特定配置文件profiles/delete_profiles.py
: 删除多个配置文件profiles/update_profile_proxy.py
: 更新配置文件的代理profiles/batch_update_proxy.py
: 更新多个配置文件的代理profiles/reset_profile_proxy.py
: 重置配置文件的代理profiles/batch_reset_profile_proxy.py
: 重置多个配置文件的代理profiles/create_profile_tags.py
: 为配置文件创建标签profiles/get_profile_tags.py
: 获取所有配置文件标签profiles/update_profile_tags.py
: 更新配置文件的标签profiles/batch_update_profile_tags.py
: 更新多个配置文件的标签profiles/clear_profile_tags.py
: 清除配置文件的所有标签profiles/batch_clear_profile_tags.py
: 清除多个配置文件的标签profiles/batch_create_profile_tags.py
: 为多个配置文件创建标签profiles/get_all_profile_groups.py
: 获取所有配置文件组profiles/change_profile_group.py
: 更改配置文件组profiles/batch_change_profile_group.py
: 批量更改配置文件组
本地数据示例
locals/clear_profile_cache.py
: 清除浏览器缓存locals/clear_profile_cookies.py
: 清除浏览器Cookie
CDP 端点示例
cdp_endpoints/connect_browser.py
: 使用CDP连接到浏览器并使用Playwright进行自动化cdp_endpoints/connect_once_browser.py
: 使用CDP连接到一次性浏览器并使用Playwright进行自动化
要运行示例:
bash
python examples/browsers/start_browser.py
python examples/browsers/start_browser.py
与Playwright的CDP集成
一个强大的功能是能够使用Chrome DevTools Protocol (CDP)连接到浏览器并使用Playwright对其进行自动化:
python
# 获取CDP WebSocket URL
websocket_url = client.cdp_endpoints.connect_browser(
profile_id="your_profile_id",
config={"headless": False}
)["data"]["webSocketDebuggerUrl"]
# 使用Playwright的URL进行自动化
from playwright.async_api import async_playwright
async def automate():
async with async_playwright() as p:
browser = await p.chromium.connect_over_cdp(websocket_url)
page = browser.contexts[0].pages[0]
await page.goto("https://example.com")
# ... 执行其他操作
# 获取CDP WebSocket URL
websocket_url = client.cdp_endpoints.connect_browser(
profile_id="your_profile_id",
config={"headless": False}
)["data"]["webSocketDebuggerUrl"]
# 使用Playwright的URL进行自动化
from playwright.async_api import async_playwright
async def automate():
async with async_playwright() as p:
browser = await p.chromium.connect_over_cdp(websocket_url)
page = browser.contexts[0].pages[0]
await page.goto("https://example.com")
# ... 执行其他操作
CDP自动化的完整示例可在examples/cdp_endpoints
目录中找到。
支持
如需支持,请随时通过Discord联系我们。有关更详细的文档,请访问Nstbrowser官方文档:Nstbrowser API文档。