Skip to content

Bộ công cụ phát triển Nstbrowser dành cho Python

Một Bộ công cụ phát triển Python để tương tác với Nstbrowser API v2

Tổng quan

Bộ công cụ phát triển này triển khai Nstbrowser API v2, đây là API được khuyến nghị với đầy đủ chức năng từ v1 cộng thêm các tính năng bổ sung. Nó cung cấp một bộ công cụ toàn diện để quản lý hồ sơ trình duyệt, điều khiển các phiên bản trình duyệt, quản lý dữ liệu trình duyệt cục bộ và sử dụng Giao thức Chrome DevTools (CDP) để tự động hóa trình duyệt.

Bộ công cụ phát triển cho phép bạn:

  • Tạo và quản lý hồ sơ trình duyệt với cấu hình dấu vân tay chi tiết
  • Khởi động và dừng các phiên bản trình duyệt riêng lẻ hoặc hàng loạt
  • Cấu hình và quản lý proxy cho hồ sơ
  • Quản lý thẻ hồ sơ để tổ chức tốt hơn
  • Xóa bộ nhớ cache và cookie của trình duyệt
  • Kết nối với trình duyệt bằng Giao thức Chrome DevTools (CDP)
  • Tự động hóa các hành động trình duyệt thông qua tích hợp CDP

Cài đặt

bash
pip install nstbrowser
pip install nstbrowser

Bắt đầu

Để sử dụng Bộ công cụ phát triển, bạn cần một khóa API từ Nstbrowser:

python
from nstbrowser import NstbrowserClient

# Khởi tạo client với khóa API của bạn
client = NstbrowserClient(api_key="your_api_key")

# Bây giờ bạn có thể sử dụng các dịch vụ khác nhau
profile_id = "your_profile_id"

# Khởi động một phiên bản trình duyệt
response = client.browsers.start_browser(profile_id=profile_id)
print(f"Trình duyệt đã khởi động: {response}")

# Dừng phiên bản trình duyệt
response = client.browsers.stop_browser(profile_id=profile_id)
print(f"Trình duyệt đã dừng: {response}")
from nstbrowser import NstbrowserClient

# Khởi tạo client với khóa API của bạn
client = NstbrowserClient(api_key="your_api_key")

# Bây giờ bạn có thể sử dụng các dịch vụ khác nhau
profile_id = "your_profile_id"

# Khởi động một phiên bản trình duyệt
response = client.browsers.start_browser(profile_id=profile_id)
print(f"Trình duyệt đã khởi động: {response}")

# Dừng phiên bản trình duyệt
response = client.browsers.stop_browser(profile_id=profile_id)
print(f"Trình duyệt đã dừng: {response}")

Dịch vụ khả dụng

Bộ công cụ phát triển cung cấp các dịch vụ sau:

BrowsersService

Quản lý các phiên bản trình duyệt, bao gồm khởi động, dừng và lấy thông tin về trình duyệt.

python
# Khởi động trình duyệt cho một hồ sơ cụ thể
response = client.browsers.start_browser(profile_id="your_profile_id")

# Khởi động nhiều trình duyệt cùng lúc
response = client.browsers.start_browsers(profile_ids=["profile_id_1", "profile_id_2"])

# Khởi động trình duyệt một lần với cấu hình tùy chỉnh
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)

# Dừng trình duyệt
response = client.browsers.stop_browser(profile_id="your_profile_id")

# Dừng nhiều trình duyệt
response = client.browsers.stop_browsers(profile_ids=["profile_id_1", "profile_id_2"])

# Lấy các trình duyệt đang hoạt động
response = client.browsers.get_browsers(status="running")

# Lấy các trang của trình duyệt
response = client.browsers.get_browser_pages(profile_id="your_profile_id")

# Lấy thông tin trình gỡ lỗi trình duyệt
response = client.browsers.get_browser_debugger(profile_id="your_profile_id")
# Khởi động trình duyệt cho một hồ sơ cụ thể
response = client.browsers.start_browser(profile_id="your_profile_id")

# Khởi động nhiều trình duyệt cùng lúc
response = client.browsers.start_browsers(profile_ids=["profile_id_1", "profile_id_2"])

# Khởi động trình duyệt một lần với cấu hình tùy chỉnh
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)

# Dừng trình duyệt
response = client.browsers.stop_browser(profile_id="your_profile_id")

# Dừng nhiều trình duyệt
response = client.browsers.stop_browsers(profile_ids=["profile_id_1", "profile_id_2"])

# Lấy các trình duyệt đang hoạt động
response = client.browsers.get_browsers(status="running")

# Lấy các trang của trình duyệt
response = client.browsers.get_browser_pages(profile_id="your_profile_id")

# Lấy thông tin trình gỡ lỗi trình duyệt
response = client.browsers.get_browser_debugger(profile_id="your_profile_id")

ProfilesService

Quản lý hồ sơ trình duyệt, bao gồm tạo, xóa, cấu hình proxy và quản lý thẻ.

python
# Tạo một hồ sơ mới
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)

# Lấy hồ sơ với bộ lọc
response = client.profiles.get_profiles(data={"page": 1, "pageSize": 10})

# Xóa một hồ sơ
response = client.profiles.delete_profile(profile_id="your_profile_id")

# Xóa nhiều hồ sơ
response = client.profiles.delete_profiles(profile_ids=["profile_id_1", "profile_id_2"])

# Cập nhật proxy của một hồ sơ
response = client.profiles.update_profile_proxy(profile_id="your_profile_id", data={"url": "http://admin:[email protected]:8000"})

# Đặt lại proxy của một hồ sơ
response = client.profiles.reset_profile_proxy(profile_id="your_profile_id")

# Quản lý thẻ hồ sơ
response = client.profiles.create_profile_tags(profile_id="your_profile_id", data=[{"name": "social", "color": "#646AEE"}])
response = client.profiles.get_profile_tags()
# Tạo một hồ sơ mới
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)

# Lấy hồ sơ với bộ lọc
response = client.profiles.get_profiles(data={"page": 1, "pageSize": 10})

# Xóa một hồ sơ
response = client.profiles.delete_profile(profile_id="your_profile_id")

# Xóa nhiều hồ sơ
response = client.profiles.delete_profiles(profile_ids=["profile_id_1", "profile_id_2"])

# Cập nhật proxy của một hồ sơ
response = client.profiles.update_profile_proxy(profile_id="your_profile_id", data={"url": "http://admin:[email protected]:8000"})

# Đặt lại proxy của một hồ sơ
response = client.profiles.reset_profile_proxy(profile_id="your_profile_id")

# Quản lý thẻ hồ sơ
response = client.profiles.create_profile_tags(profile_id="your_profile_id", data=[{"name": "social", "color": "#646AEE"}])
response = client.profiles.get_profile_tags()

LocalsService

Quản lý dữ liệu trình duyệt cục bộ, chẳng hạn như bộ nhớ cache và cookie.

python
# Xóa bộ nhớ cache trình duyệt
response = client.locals.clear_profile_cache(profile_id="your_profile_id")

# Xóa cookie trình duyệt
response = client.locals.clear_profile_cookies(profile_id="your_profile_id")
# Xóa bộ nhớ cache trình duyệt
response = client.locals.clear_profile_cache(profile_id="your_profile_id")

# Xóa cookie trình duyệt
response = client.locals.clear_profile_cookies(profile_id="your_profile_id")

CdpEndpointsService

Cung cấp kết nối với trình duyệt bằng Giao thức Chrome DevTools (CDP) để tự động hóa.

python
# Kết nối với trình duyệt bằng CDP
config = {
    "headless": False,
    "autoClose": False
}
response = client.cdp_endpoints.connect_browser(profile_id="your_profile_id", config=config)

# Kết nối với trình duyệt một lần bằng CDP
config = {
    "name": "testProfile",
    "platform": "Windows",
    "kernelMilestone": "132",
    "autoClose": False,
    "headless": False
}
response = client.cdp_endpoints.connect_once_browser(config=config)
# Kết nối với trình duyệt bằng CDP
config = {
    "headless": False,
    "autoClose": False
}
response = client.cdp_endpoints.connect_browser(profile_id="your_profile_id", config=config)

# Kết nối với trình duyệt một lần bằng CDP
config = {
    "name": "testProfile",
    "platform": "Windows",
    "kernelMilestone": "132",
    "autoClose": False,
    "headless": False
}
response = client.cdp_endpoints.connect_once_browser(config=config)

Ví dụ

Bộ công cụ phát triển đi kèm với một tập hợp các ví dụ toàn diện trong thư mục /examples, được sắp xếp theo dịch vụ:

Ví dụ về Trình duyệt

  • browsers/start_browser.py: Khởi động trình duyệt cho một hồ sơ cụ thể
  • browsers/start_browsers.py: Khởi động nhiều trình duyệt cùng lúc
  • browsers/start_once_browser.py: Khởi động trình duyệt một lần với cấu hình tùy chỉnh
  • browsers/stop_browser.py: Dừng trình duyệt
  • browsers/stop_browsers.py: Dừng nhiều trình duyệt
  • browsers/get_browsers.py: Lấy thông tin trạng thái trình duyệt
  • browsers/get_browser_pages.py: Lấy thông tin các trang của trình duyệt
  • browsers/get_browser_debugger.py: Lấy thông tin trình gỡ lỗi trình duyệt

Ví dụ về Hồ sơ

  • profiles/create_profile.py: Tạo một hồ sơ mới với cấu hình chi tiết
  • profiles/get_profiles.py: Lấy hồ sơ với các tùy chọn lọc
  • profiles/delete_profile.py: Xóa một hồ sơ cụ thể
  • profiles/delete_profiles.py: Xóa nhiều hồ sơ
  • profiles/update_profile_proxy.py: Cập nhật proxy của một hồ sơ
  • profiles/batch_update_proxy.py: Cập nhật proxy cho nhiều hồ sơ
  • profiles/reset_profile_proxy.py: Đặt lại proxy của một hồ sơ
  • profiles/batch_reset_profile_proxy.py: Đặt lại proxy cho nhiều hồ sơ
  • profiles/create_profile_tags.py: Tạo thẻ cho một hồ sơ
  • profiles/get_profile_tags.py: Lấy tất cả thẻ hồ sơ
  • profiles/update_profile_tags.py: Cập nhật thẻ cho một hồ sơ
  • profiles/batch_update_profile_tags.py: Cập nhật thẻ cho nhiều hồ sơ
  • profiles/clear_profile_tags.py: Xóa tất cả thẻ khỏi một hồ sơ
  • profiles/batch_clear_profile_tags.py: Xóa thẻ khỏi nhiều hồ sơ
  • profiles/batch_create_profile_tags.py: Tạo thẻ cho nhiều hồ sơ
  • profiles/get_all_profile_groups.py: Lấy tất cả nhóm hồ sơ
  • profiles/change_profile_group.py: Thay đổi nhóm hồ sơ
  • profiles/batch_change_profile_group.py: Thay đổi hàng loạt nhóm hồ sơ

Ví dụ về Dữ liệu cục bộ

  • locals/clear_profile_cache.py: Xóa bộ nhớ cache trình duyệt
  • locals/clear_profile_cookies.py: Xóa cookie trình duyệt

Ví dụ về Điểm cuối CDP

  • cdp_endpoints/connect_browser.py: Kết nối với trình duyệt bằng CDP và tự động hóa với Playwright
  • cdp_endpoints/connect_once_browser.py: Kết nối với trình duyệt một lần với CDP và tự động hóa với Playwright

Để chạy một ví dụ:

bash
python examples/browsers/start_browser.py
python examples/browsers/start_browser.py

Tích hợp CDP với Playwright

Một tính năng mạnh mẽ là khả năng kết nối với trình duyệt bằng Giao thức Chrome DevTools (CDP) và tự động hóa chúng bằng Playwright:

python
# Lấy URL WebSocket của CDP
websocket_url = client.cdp_endpoints.connect_browser(
    profile_id="your_profile_id", 
    config={"headless": False}
)["data"]["webSocketDebuggerUrl"]

# Sử dụng URL với Playwright để tự động hóa
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")
        # ... thực hiện các hành động khác
# Lấy URL WebSocket của CDP
websocket_url = client.cdp_endpoints.connect_browser(
    profile_id="your_profile_id", 
    config={"headless": False}
)["data"]["webSocketDebuggerUrl"]

# Sử dụng URL với Playwright để tự động hóa
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")
        # ... thực hiện các hành động khác

Các ví dụ hoàn chỉnh cho tự động hóa CDP có sẵn trong thư mục examples/cdp_endpoints.

Hỗ trợ

Để được hỗ trợ, hãy liên hệ với chúng tôi qua Discord. Để biết thêm tài liệu chi tiết, hãy truy cập tài liệu chính thức của Nstbrowser: Tài liệu API Nstbrowser.