【问题标题】:Search all of Google with Google Python API使用 Google Python API 搜索所有 Google
【发布时间】:2016-05-09 14:50:57
【问题描述】:

我将使用 python。我的计划是做一个搜索一堆东西的程序,看看谷歌有多少搜索结果。但我只能弄清楚如何让自定义搜索引擎发挥作用。

在 python 中,如何使用 Google API 使用 Google 的主要搜索引擎进行简单搜索?据我了解,在过去几年中,随着谷歌推动谷歌应用引擎,这个问题的答案发生了变化。

【问题讨论】:

标签: python google-app-engine google-search google-custom-search google-search-api


【解决方案1】:

最近我也在寻找 Google Search API,但被很多过时的信息误导了。这是我在 Google Developers 网站上找到的内容:https://developers.google.com/api-client-library/python/apis/customsearch/v1

根据文档,您的功能将类似于

from googleapiclient.discovery import build


def google_results_count(query):
    service = build("customsearch", "v1",
                    developerKey="[put your API key here]")

    result = service.cse().list(
            q=query,
            cx='[put your CSE key here']
        ).execute()

    return result["searchInformation"]["totalResults"]

print google_results_count('Python is awesome')

不幸的是,使用 CSE API 会为您提供与使用网络搜索获得的结果计数不同的结果。在上面的示例中,我在 Python 中得到了 2 680 000 和大约。 21 000 000 用于 Google.com 上的相同查询 原因如下:https://support.google.com/customsearch/answer/70392?hl=en

获取 API 和 CSE 密钥以及 CSE 的所有限制是另一回事,我强烈建议您查看以下答案:https://stackoverflow.com/a/11206266/1704272 和下面的下一个答案。

另一种方法是解析来自 Google.com 的 HTML 响应,这将为您提供最完整的结果,但它不是很可靠,因为 Google 更改了 HTML 标记。更重要的是,这违反了他们的 TOS,更多内容请在此处阅读:Is it ok to scrape data from Google results?

我的结论。 您有三个选择:

  1. 使用 Google CSE API(免费)。使用这个,如果你需要保持合法并且你确定你不会超过限制。不能在公共应用中使用。
  2. 使用付费 API(Google 或任何其他,更便宜)。将其用于任何公共应用程序都是合法的,但请准备好为此付费。
  3. 抓取谷歌网页。这将为您提供最佳结果,但我只会将此选项用于私人需求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多