跳到主要内容
Open In ColabOpen on GitHub

BrightDataWebScraperAPI

Bright Data 提供了一个强大的网页抓取API,可让您从 100 多个热门域名提取结构化数据,包括亚马逊产品详情、LinkedIn 个人资料等,这对于需要可靠结构化网页数据流的 AI 代理来说特别有用。

概述

集成详情

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

工具功能

原生异步返回工件返回数据定价
来自网站的结构化数据(亚马逊产品、LinkedIn 个人资料等)需要 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 工具。此工具允许您从各种网站提取结构化数据,包括亚马逊产品详情、LinkedIn 个人资料等,通过 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)

自定义选项

BrightDataWebScraperAPI 工具接受多个参数用于自定义

参数类型描述
urlstr用于提取数据的 URL
dataset_typestr要使用的数据集类型(例如,“amazon_product”)
zipcodestr可选的邮政编码,用于获取特定位置的数据

可用数据集类型

此工具支持以下数据集类型用于结构化数据提取

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

在代理中使用

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 参考