用Google Python API搜索所有Google [英] Search all of Google with Google Python API

查看:153
本文介绍了用Google Python API搜索所有Google的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用python。我的计划是制作一个搜索一堆东西的程序,并查看谷歌有多少搜索结果。但我只能弄清楚如何让自定义搜索引擎进行某种工作。

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

解决方案

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



根据文档,您的函数将类似于

p>

  from googleapiclient.discovery import build 


def google_results_count(查询):
service = build(customsearch,v1,
developerKey =[把你的API key放在这里])

result = service.cse()。list(
q = query,
cx ='[把你的CSE键放在这里']
).execute()

返回结果[searchInformation] [totalResults]

print google_results_count('Python is awesome')

不幸的是,使用CSE API会给你从使用网页搜索获得的结果数量不同。在上面的例子中,我得到了2 680 000 Python和约。对于Google.com上的相同查询,21 000 000美元
以下说明原因: https://support.google.com/customsearch/answer/70392?hl=zh_CN



获取API和CSE密钥以及所有CSE的局限性是一个完全不同的故事,我强烈建议你看一下这个答案: https://stackoverflow.com/a/11206266/另一种方法是解析来自Google.com的HTML响应,它会给你提供最完整的结果,但是,它不是很可靠,因为Google更改了HTML标记。更重要的是,这是违反他们的服务条款,更多在这里阅读:可以从Google搜索结果中提取数据吗?



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


  1. 使用Google CSE API(免费)。如果您需要保持合法,并且您确定不会超出限制,请使用此功能。无法在公共应用程序中使用。

  2. 使用付费API(Google或任何其他更便宜)。将此用于任何公共应用程序是合法的,但要准备为此付费。

  3. 刮取Google网页。这会给你最好的结果,但我只会用这个选项来满足私人需求。


I will be using python. My plan is to make a program that searches a bunch of things, and sees how many search results google has for it. But I can only figure out how to get custom search engine to kind of work.

In python, how do I use the Google API to do a simple search using Google's main search engine? As I understand, the answer to this has changed within in the last few years as google has made a push to the google app engine.

解决方案

Recently I was also looking for Google Search API and was misguided by a lot of outdated information. Here is what I found on Google Developers website: https://developers.google.com/api-client-library/python/apis/customsearch/v1

According to the docs your function will be something like

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')

Unfortunately, using CSE API will give you different result count from the one you get using web search. In the example above I got 2 680 000 in Python and approx. 21 000 000 for the same query on Google.com Here is an explanation why: https://support.google.com/customsearch/answer/70392?hl=en

Getting the API and CSE keys and all the limitations of CSE is a whole different story, I highly recommend you looking at this answer: https://stackoverflow.com/a/11206266/1704272 and the next one below for the alternatives.

Another approach is to parse the HTML response from Google.com which will give you the most complete results but it is not very reliable because Google changes the HTML markup. And more important this is against their TOS, more to read here: Is it ok to scrape data from Google results?

My conclusion. You have three options:

  1. Use Google CSE API (free). Use this, if you need to stay legal and you are sure you won't exceed the limit. Can not be used in public application.
  2. Use paid API (Google or any other, less expensive). It is legal to use this for any public application but be ready to pay for that.
  3. Scrape Google web page. This will give you the best results but I would use this option only for private needs.

这篇关于用Google Python API搜索所有Google的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆