> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Oxylabs 集成

> 使用 LangChain Python 与 Oxylabs 工具集成。

> [Oxylabs](https://oxylabs.io/) 是一个市场领先的网络情报收集平台，遵循最高的商业、道德和合规标准，使全球公司能够解锁数据驱动的洞察。

## 概述

此包包含与 Oxylabs 的 LangChain 集成，提供使用 LangChain 框架通过 Oxylabs Web Scraper API 抓取 Google 搜索结果的工具。

此包提供以下类：

* `OxylabsSearchRun` - 一个返回格式化文本格式的已抓取 Google 搜索结果的工具
* `OxylabsSearchResults` - 一个返回 JSON 格式已抓取 Google 搜索结果的工具
* `OxylabsSearchAPIWrapper` - 用于初始化 Oxylabs API 的 API 包装器

|         定价         |
| :----------------: |
| ✅ 1 周内免费 5,000 条结果 |

## 设置

安装所需的依赖项。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-oxylabs
```

### 凭据

设置正确的 API 密钥和环境变量。创建您的 API 用户凭据：在 [Oxylabs 仪表板](https://dashboard.oxylabs.io/en/registration) 注册免费试用或购买产品以创建您的 API 用户凭据（OXYLABS\_USERNAME 和 OXYLABS\_PASSWORD）。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

os.environ["OXYLABS_USERNAME"] = getpass.getpass("Enter your Oxylabs username: ")
os.environ["OXYLABS_PASSWORD"] = getpass.getpass("Enter your Oxylabs password: ")
```

## 实例化

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_oxylabs import OxylabsSearchAPIWrapper, OxylabsSearchRun

oxylabs_wrapper = OxylabsSearchAPIWrapper()
tool_ = OxylabsSearchRun(wrapper=oxylabs_wrapper)
```

## 调用

### 直接使用参数调用

`OxylabsSearchRun` 工具接受单个 "query" 参数，应为自然语言查询，并返回组合字符串格式的结果：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tool_.invoke({"query": "Restaurants in Paris."})
```

### 使用 ToolCall 调用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tool_ = OxylabsSearchRun(
    wrapper=oxylabs_wrapper,
    kwargs={
        "result_categories": [
            "local_information",
            "combined_search_result",
        ]
    },
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from pprint import pprint

model_generated_tool_call = {
    "args": {
        "query": "Visit restaurants in Vilnius.",
        "geo_location": "Vilnius,Lithuania",
    },
    "id": "1",
    "name": "oxylabs_search",
    "type": "tool_call",
}
tool_call_result = tool_.invoke(model_generated_tool_call)

# The content is a JSON string of results
pprint(tool_call_result.content)
```

## 在代理中使用

安装所需的依赖项。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU "langchain[openai]" langgraph
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

from langchain.chat_models import init_chat_model

os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
model = init_chat_model("gpt-4.1-mini", model_provider="openai")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain.agents import create_agent


# Initialize OxylabsSearchRun tool
tool_ = OxylabsSearchRun(wrapper=oxylabs_wrapper)

agent = create_agent(model, [tool_])

user_input = "What happened in the latest Burning Man floods?"

for step in agent.stream(
    {"messages": user_input},
    stream_mode="values",
):
    step["messages"][-1].pretty_print()
```

## JSON 结果

`OxylabsSearchResults` 工具可用作 `OxylabsSearchRun` 的替代方案，以 JSON 格式检索结果：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import json

from langchain_oxylabs import OxylabsSearchResults

tool_ = OxylabsSearchResults(wrapper=oxylabs_wrapper)

response_results = tool_.invoke({"query": "What are the most famous artists?"})
response_results = json.loads(response_results)

for result in response_results:
    for key, value in result.items():
        print(f"{key}: {value}")
```

***

## API 参考

有关此集成包的更多信息，请在此处查看：[github.com/oxylabs/langchain-oxylabs](https://github.com/oxylabs/langchain-oxylabs)

Oxylabs Web Scraper API 文档：[developers.oxylabs.io/scraper-apis/web-scraper-api](https://developers.oxylabs.io/scraper-apis/web-scraper-api)

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\tools\oxylabs.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
