谷歌搜索API - 获得“常规”谷歌搜索结果

时间:2013-05-06 19:29:11

标签: rest google-custom-search

我想进行一些Google搜索并获得与我在www.google.se上搜索相同的结果(我在瑞典)。我在这里创建了一个Google API帐户来获取API密钥https://code.google.com/apis/console/b/0/

我这样称呼API:

https://www.googleapis.com/customsearch/v1?key=KEY&cx=013036536707430787589:_pqjad5hr1a&gl=se&cr=se&googlehost=google.se&q=bästa espressomaskin&alt=json

然而,这与我通常会搜索“bästathosomaskin”的结果不同,我无法弄清楚原因。

我的第二个问题是关于参数“cx”。实际上是什么,它做了什么?我刚刚使用了Google API简介网站提供的那个。

我还想要超过10个结果,我的API控制台帐户上有一些钱,但是如何才能将“num”参数设置为10以上呢?

由于

2 个答案:

答案 0 :(得分:2)

您可以使用 SerpApi 访问从 常规 Google 搜索结果中提取的数据。

curl 一起使用的示例。

curl -s 'https://serpapi.com/search?q=coffee&location=Sweden&google_domain=google.se&gl=se&hl=sv&num=100'

这是通过 google-search-results 包使用它的示例。

from serpapi import GoogleSearch
import os

params = {
    "engine": "google",
    "q": "coffee",
    # "q": "bästa espressomaskin",
    "location": "Sweden",
    "google_domain": "google.se",
    "gl": "se",
    "hl": "sv",
    "num": 100,
    "api_key": os.getenv("API_KEY")
}

client = GoogleSearch(params)
data = client.get_dict()

print("Organic results")

for result in data['organic_results']:
    print(f"""
Title: {result['title']}
Link: {result['link']}
Position: {result['position']}
Snippet: {result['snippet']}
""")

检查 SerpApi playgrounddocumentation 中的其他提取数据。

免责声明:我在 SerpApi 工作。

答案 1 :(得分:1)

cx是您注册自定义搜索服务时获得的Google密钥。它允许您自定义搜索。有关详细信息,请参阅此处:https://developers.google.com/custom-search/v1/getting_started

至于结果,Google在其文档中说您的结果可能会有所不同。有关详细信息,请参阅此处:http://support.google.com/customsearch/bin/answer.py?hl=en&answer=2633385