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

# Google 搜索集成

> 本笔记本介绍了如何使用谷歌搜索组件。

本笔记本介绍了如何使用谷歌搜索组件。

首先，您需要设置正确的 API 密钥和环境变量。要设置这些，请在 Google Cloud 凭证控制台（[console.cloud.google.com/apis/credentials](https://console.cloud.google.com/apis/credentials)）中创建 `GOOGLE_API_KEY`，并使用可编程搜索引擎（[programmablesearchengine.google.com/controlpanel/create](https://programmablesearchengine.google.com/controlpanel/create)）创建 GOOGLE\_CSE\_ID。接下来，建议您遵循 [程序化谷歌搜索说明](https://stackoverflow.com/questions/37083058/programmatically-searching-google-in-python-using-custom-search)。

然后我们需要设置一些环境变量。

```python theme={null}
pip install -qU  langchain-google-community
```

```python theme={null}
import os

os.environ["GOOGLE_CSE_ID"] = ""
os.environ["GOOGLE_API_KEY"] = ""
```

```python theme={null}
from langchain.tools import tool
from langchain_google_community import GoogleSearchAPIWrapper

search = GoogleSearchAPIWrapper()


@tool
def google_search(query: str) -> str:
    """Search Google for recent results."""
    return search.run(query)
```

```python theme={null}
google_search.invoke("Obama's first name?")
```

```text theme={null}
"STATE OF HAWAII. 1 Child's First Name. (Type or print). 2. Sex. BARACK. 3. This Birth. CERTIFICATE OF LIVE BIRTH. FILE. NUMBER 151 le. lb. Middle Name. Barack Hussein Obama II is an American former politician who served as the 44th president of the United States from 2009 to 2017. A member of the Democratic\xa0... When Barack Obama was elected president in 2008, he became the first African American to hold ... The Middle East remained a key foreign policy challenge. Jan 19, 2017 ... Jordan Barack Treasure, New York City, born in 2008 ... Jordan Barack Treasure made national news when he was the focus of a New York newspaper\xa0... Portrait of George Washington, the 1st President of the United States ... Portrait of Barack Obama, the 44th President of the United States\xa0... His full name is Barack Hussein Obama II. Since the “II” is simply because he was named for his father, his last name is Obama. Mar 22, 2008 ... Barry Obama decided that he didn't like his nickname. A few of his friends at Occidental College had already begun to call him Barack (his\xa0... Aug 18, 2017 ... It took him several seconds and multiple clues to remember former President Barack Obama's first name. Miller knew that every answer had to\xa0... Feb 9, 2015 ... Michael Jordan misspelled Barack Obama's first name on 50th-birthday gift ... Knowing Obama is a Chicagoan and huge basketball fan,\xa0... 4 days ago ... Barack Obama, in full Barack Hussein Obama II, (born August 4, 1961, Honolulu, Hawaii, U.S.), 44th president of the United States (2009–17) and\xa0..."
```

## 结果数量

您可以使用 `k` 参数来设置结果数量

```python theme={null}
search = GoogleSearchAPIWrapper(k=1)


@tool
def im_feeling_lucky(query: str) -> str:
    """Search Google and return the first result."""
    return search.run(query)
```

```python theme={null}
im_feeling_lucky.invoke("python")
```

```text theme={null}
'The official home of the Python Programming Language.'
```

'The official home of the Python Programming Language.'

## 元数据结果

通过 GoogleSearch 运行查询并返回片段、标题和链接元数据。

* 片段：结果的描述。
* 标题：结果的标题。
* 链接：结果的链接。

```python theme={null}
search = GoogleSearchAPIWrapper()


@tool
def google_search_snippets(query: str) -> str:
    """Search Google for recent results."""
    return search.results(query, 5)
```

```python theme={null}
```

※ 重要提示：在您之前的输出中，占位符设置不正确。缺失的 ID：\['0']; 预期之外的额外 ID：\[]。请再次翻译全文，确保每个占位符（六边形括号 + 数字）都保留在原始位置，且只出现一次。

***

> 官方原文：[src/oss/python/integrations/tools/google\_search.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/tools/google_search.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.