BrightDataWebScraperAPI
Bright Data 提供了一个强大的 Web Scraper API,可以让你从 100 多个热门域名提取结构化数据,包括 Amazon 产品详情、LinkedIn 个人资料等,这对于需要可靠结构化网络数据源的 AI 代理来说尤其有用。
概述
集成详情
| 类名 | 包名 | 可序列化 | JS 支持 | 包最新版本 |
|---|---|---|---|---|
| ✅ | ❌ |
工具特性
| 原生异步支持 | 返回工件 | 返回数据 | 定价 |
|---|---|---|---|
| ❌ | ❌ | 网站结构化数据(亚马逊产品、领英个人资料等) | 需要 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:
| Parameter | Type | Description |
|---|---|---|
url | str | The URL to extract data from |
dataset_type | str | Type of dataset to use (e.g., "amazon_product") |
zipcode | str | Optional 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 Reference:ChatGoogleGenerativeAI | create_react_agent
API 参考
Related
- Tool conceptual guide
- Tool how-to guides