如何通过Ruby中的API从关键字中检索前10个Google搜索结果?

时间:2022-08-22 10:19:20

I'm a little confused as to what is the best way to retrieve the top 10 Google results for a specific search/keyword. I just need the title and url (description is not essential).

对于检索特定搜索/关键字的前10个Google搜索结果的最佳方法,我有点困惑。我只需要标题和网址(描述不是必需的)。

I'm using Ruby and apparently there was a great way to do it with the googleajax gem. I've been able to get it to work but am concerned that it's a deprecated API and could be phased out any day. Also, the workaround to get more than 4 results at a time isn't very clean.

我正在使用Ruby,显然有一个很好的方法来使用googleajax gem。我已经能够让它工作,但我担心它是一个已弃用的API,可能会在任何一天被逐步淘汰。此外,一次获得4个以上结果的解决方法并不是很干净。

I think the Google Custom Search might be an option but the daily limit of 100 queries is restricting. I would prefer to not scrape Google as it's a violation of their terms.

我认为谷歌自定义搜索可能是一个选项,但100个查询的每日限制是有限的。我宁愿不刮谷歌,因为这违反了他们的条款。

What other options do I have to make this work? Any json/ruby/rails option would work for me. Thank you!

我还有什么其他选择才能使这项工作成功?任何json / ruby​​ / rails选项对我都有用。谢谢!

5 个解决方案

#1


0  

You're not very explicit in your question about the trade offs you're willing to make, But you might want to think about this more:

你在关于你愿意做出的权衡的问题上并不是很明确,但你可能想要考虑更多:

I think the Google Custom Search might be an option but the daily limit of 100 queries is restricting. I would prefer to not scrape Google as it's a violation of their terms.

我认为谷歌自定义搜索可能是一个选项,但100个查询的每日限制是有限的。我宁愿不刮谷歌,因为这违反了他们的条款。

I've used google custom search, and it is very easy but the limit is in place. If you are concerned about not violating Google's TOS, this is the only way to go. You need to decide if you're willing to violate the TOS, and if not you should just use the google custom search.

我使用谷歌自定义搜索,它很容易但限制到位。如果您担心不违反Google的服务条款,这是唯一的方法。你需要决定是否愿意违反服务条款,如果不是,你应该只使用谷歌自定义搜索。

#2


0  

I recommend use 'rest-client' gem.

我建议使用'rest-client'gem。

RestClient.get 'google_api_url' 

#3


0  

it occurred to me first, just example:

它首先发生在我身上,只是例子:

require 'open-uri'
require 'nokogiri'
require 'restclient'

words = ["Foo", "Bar", "Baz"]
staff = [].tap do |acc|
  words.each do |word|
    link = "https://www.google.com/search?q=#{word}"
    page = Nokogiri::HTML(RestClient.get(link))
    page.css('a')[27].text
    .....................# <- and parse data what you need
  end
end

#4


0  

If your requirement is more than 100 queries on a single day, but not on a daily/regular basis and if Google results are not a must have, you can consider using Bing Search API.

如果您的要求在一天内超过100个查询,但不是每天/定期查询,并且如果Google结果不是必须的,您可以考虑使用Bing Search API。

The Bing Search API allows for 5000 queries a month, all of which you can choose to use on a single day itself. Again, this will solve your problem if your requirement is not a daily/regular one and you can look beyond Google.

Bing Search API每月允许5000次查询,您可以选择在一天内使用这些查询。同样,如果您的要求不是每日/常规要求,并且您可以超越Google,这将解决您的问题。

Else, paying Google is your only way out.

否则,支付谷歌是你唯一的出路。

#5


0  

We ended having the same issue, and we built our own gem with our own backend. It's pretty simple to use:

我们结束了同样的问题,我们用自己的后端构建了自己的gem。使用起来非常简单:

query = GoogleSearchResults.new q: "coffee"
hash_results = query.get_hash

https://github.com/serpapi/google-search-results-ruby

https://github.com/serpapi/google-search-results-ruby

#1


0  

You're not very explicit in your question about the trade offs you're willing to make, But you might want to think about this more:

你在关于你愿意做出的权衡的问题上并不是很明确,但你可能想要考虑更多:

I think the Google Custom Search might be an option but the daily limit of 100 queries is restricting. I would prefer to not scrape Google as it's a violation of their terms.

我认为谷歌自定义搜索可能是一个选项,但100个查询的每日限制是有限的。我宁愿不刮谷歌,因为这违反了他们的条款。

I've used google custom search, and it is very easy but the limit is in place. If you are concerned about not violating Google's TOS, this is the only way to go. You need to decide if you're willing to violate the TOS, and if not you should just use the google custom search.

我使用谷歌自定义搜索,它很容易但限制到位。如果您担心不违反Google的服务条款,这是唯一的方法。你需要决定是否愿意违反服务条款,如果不是,你应该只使用谷歌自定义搜索。

#2


0  

I recommend use 'rest-client' gem.

我建议使用'rest-client'gem。

RestClient.get 'google_api_url' 

#3


0  

it occurred to me first, just example:

它首先发生在我身上,只是例子:

require 'open-uri'
require 'nokogiri'
require 'restclient'

words = ["Foo", "Bar", "Baz"]
staff = [].tap do |acc|
  words.each do |word|
    link = "https://www.google.com/search?q=#{word}"
    page = Nokogiri::HTML(RestClient.get(link))
    page.css('a')[27].text
    .....................# <- and parse data what you need
  end
end

#4


0  

If your requirement is more than 100 queries on a single day, but not on a daily/regular basis and if Google results are not a must have, you can consider using Bing Search API.

如果您的要求在一天内超过100个查询,但不是每天/定期查询,并且如果Google结果不是必须的,您可以考虑使用Bing Search API。

The Bing Search API allows for 5000 queries a month, all of which you can choose to use on a single day itself. Again, this will solve your problem if your requirement is not a daily/regular one and you can look beyond Google.

Bing Search API每月允许5000次查询,您可以选择在一天内使用这些查询。同样,如果您的要求不是每日/常规要求,并且您可以超越Google,这将解决您的问题。

Else, paying Google is your only way out.

否则,支付谷歌是你唯一的出路。

#5


0  

We ended having the same issue, and we built our own gem with our own backend. It's pretty simple to use:

我们结束了同样的问题,我们用自己的后端构建了自己的gem。使用起来非常简单:

query = GoogleSearchResults.new q: "coffee"
hash_results = query.get_hash

https://github.com/serpapi/google-search-results-ruby

https://github.com/serpapi/google-search-results-ruby