对于检索特定搜索/关键字的前10个Google搜索结果的最佳方法,我有点困惑。我只需要标题和网址(描述不是必需的)。
我正在使用Ruby,显然有一个很好的方法来使用googleajax gem。我已经能够让它工作,但我担心它是一个已弃用的API,可能会在任何一天被逐步淘汰。此外,一次获得4个以上结果的解决方法并不是很干净。
我认为谷歌自定义搜索可能是一个选项,但100个查询的每日限制是有限的。我宁愿不刮谷歌,因为这违反了他们的条款。
我有什么其他选择才能使这项工作?任何json / ruby / rails选项对我都有用。谢谢!
你在关于你愿意做出的权衡的问题上你并不是很明确,但你可能想要考虑更多:
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.
我使用谷歌自定义搜索,它很容易但限制到位。如果您担心不违反Google的服务条款,这是唯一的方法。你需要决定是否愿意违反服务条款,如果不是,你应该只使用谷歌自定义搜索。
我建议使用'rest-client'gem。
RestClient.get 'google_api_url'
它首先发生在我身上,只是例子:
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
如果您的要求在一天内超过100个查询,但不是每天/定期查询,并且如果Google结果不是必须的,您可以考虑使用Bing Search API。
Bing Search API每月允许5000次查询,您可以选择在一天内使用这些查询。同样,如果您的要求不是每日/常规要求,并且您可以超越Google,这将解决您的问题。
否则,支付谷歌是你唯一的出路。
我们结束了同样的问题,我们用自己的后端构建了自己的gem。使用起来非常简单:
query = GoogleSearchResults.new q: "coffee"
hash_results = query.get_hash