Skip to main content
Open In ColabOpen on GitHub

BrightDataWebScraperAPI

Bright Data 提供了一个强大的 Web Scraper API,可以让你从 100 多个热门域名提取结构化数据,包括 Amazon 产品详情、LinkedIn 个人资料等,这对于需要可靠结构化网络数据源的 AI 代理来说尤其有用。

概述

集成详情

类名包名可序列化JS 支持包最新版本
BrightDataWebScraperAPIlangchain-brightdataPyPI - Version

工具特性

原生异步支持返回工件返回数据定价
网站结构化数据(亚马逊产品、领英个人资料等)需要 Bright Data 账户

设置

集成位于 langchain-brightdata 包中。

pip install langchain-brightdata

您需要一个 Bright Data API 密钥才能使用此工具。您可以将其设置为环境变量:

import os

os.environ["BRIGHT_DATA_API_KEY"] = "your-api-key"

或者直接在初始化工具时传入:

from langchain_brightdata import BrightDataWebScraperAPI

scraper_tool = BrightDataWebScraperAPI(bright_data_api_key="your-api-key")

实例化

此处展示了如何实例化一个 BrightDataWebScraperAPI 工具实例。该工具允许您从各类网站提取结构化数据,包括亚马逊产品详情、领英个人主页等,全部使用 Bright Data 的数据集 API。

该工具在实例化时接受多个参数:

  • bright_data_api_key (必需, str): 您的 Bright Data API 密钥,用于身份验证。
  • dataset_mapping (可选, Dict[str, str]): 一个字典,用于将数据集类型映射到其对应的 Bright Data 数据集 ID。默认映射包括:
    • "amazon_product": "gd_l7q7dkf244hwjntr0"
    • "amazon_product_reviews": "gd_le8e811kzy4ggddlq"
    • "linkedin_person_profile": "gd_l1viktl72bvl7bjuj0"
    • "linkedin_company_profile": "gd_l1vikfnt1wgvvqz95w"

调用

基本用法

from langchain_brightdata import BrightDataWebScraperAPI

# Initialize the tool
scraper_tool = BrightDataWebScraperAPI(
bright_data_api_key="your-api-key" # Optional if set in environment variables
)

# Extract Amazon product data
results = scraper_tool.invoke(
{"url": "https://www.amazon.com/dp/B08L5TNJHG", "dataset_type": "amazon_product"}
)

print(results)

带参数的高级用法

from langchain_brightdata import BrightDataWebScraperAPI

# Initialize with default parameters
scraper_tool = BrightDataWebScraperAPI(bright_data_api_key="your-api-key")

# Extract Amazon product data with location-specific pricing
results = scraper_tool.invoke(
{
"url": "https://www.amazon.com/dp/B08L5TNJHG",
"dataset_type": "amazon_product",
"zipcode": "10001", # Get pricing for New York City
}
)

print(results)

# Extract LinkedIn profile data
linkedin_results = scraper_tool.invoke(
{
"url": "https://www.linkedin.com/in/satyanadella/",
"dataset_type": "linkedin_person_profile",
}
)

print(linkedin_results)

Customization Options

The BrightDataWebScraperAPI tool accepts several parameters for customization:

ParameterTypeDescription
urlstrThe URL to extract data from
dataset_typestrType of dataset to use (e.g., "amazon_product")
zipcodestrOptional zipcode for location-specific data

可用的数据集类型

该工具支持以下结构化数据提取的数据集类型:

数据集类型描述
amazon_product提取详细的亚马逊产品数据
amazon_product_reviews提取亚马逊产品评论
linkedin_person_profile提取领英个人资料数据
linkedin_company_profile提取领英公司资料数据

在 Agent 中使用

from langchain_brightdata import BrightDataWebScraperAPI
from langchain_google_genai import ChatGoogleGenerativeAI
from langgraph.prebuilt import create_react_agent

# Initialize the LLM
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", google_api_key="your-api-key")

# Initialize the Bright Data Web Scraper API tool
scraper_tool = BrightDataWebScraperAPI(bright_data_api_key="your-api-key")

# Create the agent with the tool
agent = create_react_agent(llm, [scraper_tool])

# Provide a user query
user_input = "Scrape Amazon product data for https://www.amazon.com/dp/B0D2Q9397Y?th=1 in New York (zipcode 10001)."

# Stream the agent's step-by-step output
for step in agent.stream(
{"messages": user_input},
stream_mode="values",
):
step["messages"][-1].pretty_print()

API 参考