如何在Python中使用Google自定义搜索引擎API?

2021年11月16日21:23:54 发表评论 1,090 次浏览

如何使用Google自定义搜索引擎API?学习如何创建自己的 Google 自定义搜索引擎并在 Python 中使用其应用程序编程接口 (API)。

Google 自定义搜索引擎 (CSE) 是一种适合开发人员的搜索引擎,它允许你在应用程序中包含搜索引擎,无论是网站、移动应用程序还是其他任何应用程序。

Python如何使用Google自定义搜索引擎API?由于手动抓取 Google 搜索是非常不建议的,因为它会限制每隔几个查询使用 reCAPTCHA,在本教程中,你将学习如何设置 CSE 并在 Python 中使用其 API。

在 CSE 中,你可以自定义在特定网站上搜索结果的引擎,也可以仅使用你的网站。但是,在本教程中,我们将启用我们的搜索引擎来搜索整个网络。

还学习: 如何在 Python 中使用 Google Drive API。

设置 CSE

Python Google自定义搜索引擎API用法示例?首先,要设置搜索引擎,你需要有一个 Google 帐户。之后,前往CSE 页面并登录自定义搜索引擎,如下图所示:

如何在Python中使用Google自定义搜索引擎API?

登录 Google 帐户后,你会看到一个新面板,如下所示:

如何在Python中使用Google自定义搜索引擎API?

你可以包含要包含搜索结果的网站、选择搜索引擎的语言并设置其名称。完成后,你将被重定向到此页面:

如何在Python中使用Google自定义搜索引擎API?

在 Python 中使用 CSE API

如何使用Google自定义搜索引擎API?现在要在 Python 中使用你的搜索引擎,你需要做两件事:首先,你需要获取你的搜索引擎 ID,你可以在 CSE 控制面板中轻松找到它:

如何在Python中使用Google自定义搜索引擎API?

其次,你必须生成一个新的 API 密钥,前往自定义搜索 JSON API 页面,然后单击那里的“获取密钥”按钮,将出现一个新窗口,你需要创建一个新项目(你可以将其命名为任何名称)你想要)并点击下一步按钮,之后你将拥有你的 API 密钥,这是我的结果:

如何在Python中使用Google自定义搜索引擎API?

最后,如果你想搜索整个网络,你需要在控制面板上启用它:

如何在Python中使用Google自定义搜索引擎API?现在你拥有在 Python 代码中使用 CSE API 所需的一切,打开一个新的 Python 文件并继续操作,为了方便起见,我们将使用请求库,你可以使用以下命令安装它:

pip3 install requests

让我们初始化我们的 CSE 要求:

import requests

# get the API KEY here: https://developers.google.com/custom-search/v1/overview
API_KEY = "<INSERT_YOUR_API_KEY_HERE>"
# get your Search Engine ID on your CSE control panel
SEARCH_ENGINE_ID = "<INSERT_YOUR_SEARCH_ENGINE_ID_HERE>"

出于演示目的,我们将执行一个简单的搜索查询,“python”。让我们构建我们将请求的 API URL:

# the search query you want
query = "python"
# using the first page
page = 1
# constructing the URL
# doc: https://developers.google.com/custom-search/v1/using_rest
# calculating start, (page=2) => (start=11), (page=3) => (start=21)
start = (page - 1) * 10 + 1
url = f"https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={SEARCH_ENGINE_ID}&q={query}&start={start}"

在这里,我们正在构建我们将向其发出请求的 URL。默认情况下,CSE API 返回前 10 个搜索结果,page例如将数字更改为 2,将startAPI 参数设置为 11,因此它将返回第二页结果,依此类推。

发出 API 请求并使用请求的json()方法 将返回的JSON数据自动解析为 Python 字典:

# make the API request
data = requests.get(url).json()

Python Google自定义搜索引擎API用法示例?现在,这个数据是一个包含很多结果标签的字典,我们只对“items”感兴趣,即搜索结果,默认情况下,CSE会返回10个搜索结果,让我们迭代它们:

# get the result items
search_items = data.get("items")
# iterate over 10 results found
for i, search_item in enumerate(search_items, start=1):
    # get the page title
    title = search_item.get("title")
    # page snippet
    snippet = search_item.get("snippet")
    # alternatively, you can get the HTML snippet (bolded keywords)
    html_snippet = search_item.get("htmlSnippet")
    # extract the page url
    link = search_item.get("link")
    # print the results
    print("="*10, f"Result #{i+start-1}", "="*10)
    print("Title:", title)
    print("Description:", snippet)
    print("URL:", link, "\n")

我们只是提取标题、片段(即描述)和结果页面的链接,检查结果:

========== Result #1 ==========
Title: Welcome to Python.org
Description: The official home of the Python Programming Language.
URL: https://www.python.org/

========== Result #2 ==========
Title: The Python Tutorial — Python 3.8.2 documentation
Description: It has efficient high-level data structures and a simple but effective approach to 
object-oriented programming. Python's elegant syntax and dynamic typing,
together ...
URL: https://docs.python.org/3/tutorial/

========== Result #3 ==========
Title: Download Python | Python.org
Description: Looking for Python with a different OS? Python for Windows, Linux/UNIX, Mac OS     
X, Other. Want to help test development versions of Python? Prereleases ...
URL: https://www.python.org/downloads/

太棒了,我只将输出剪切为3 个搜索结果,但它会在你的情况下打印10 个结果。

结论

Python如何使用Google自定义搜索引擎API?你可以指定各种请求参数来自定义你的搜索,还可以打印数据字典以查看其他元数据,例如总结果、搜索时间,甚至每个页面的元标记。查看CSE 的文档以获取更多详细信息。

默认情况下,对于免费访问,每天限制为100 个搜索查询,如果你觉得这对于你的应用程序来说还不够,请考虑在 API 控制台中注册计费。

此 API 的一个很好的用例是通过特定关键字获得特定页面的 Google 排名,请查看我向你展示的教程!

如何使用Google自定义搜索引擎API?现在你可以将搜索技术集成到你的应用程序中,我希望本教程对你有所帮助且易于理解。如果你喜欢它,请不要犹豫,分享它!

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: