如何获取谷歌搜索结果链接并使用mechanize将它们存储在数组中

时间:2011-05-24 18:28:25

标签: ruby-on-rails ruby nokogiri mechanize

我想使用mechanize获得10个谷歌搜索结果链接(href)所以我写了这段代码,但代码没有返回正确的谷歌搜索结果,我应该写什么?

    @searchword = params[:q]
    @sitesurl = Array.new
    agent = Mechanize.new
    page = agent.get("http://www.google.com")
    search_form = page.form_with(:name => "f")
    search_form.field_with(:name => "q").value = @searchword.to_s 
    search_results = agent.submit(search_form)
    count = 0
    c = 0
    while  c < 10
    if (search_results/"li")[count].attributes['class'].to_s == "g knavi"
      site = (search_results/"li")[count]
      code = (site/"a")[0].attributes['href']
      @sitesurl << code
      c += 1
    end
    count += 1
end

4 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

@searchword = params[:q]
@sitesurl = Array.new
agent = Mechanize.new
page = agent.get("http://www.google.com")
search_form = page.form_with(:name => "f")
search_form.field_with(:name => "q").value = @searchword.to_s     
search_results = agent.submit(search_form)

(search_results/"li.g").each do |result|
  @sitesurl << (result/"a").first.attribute('href') if result.attribute('class').to_s == 'g knavi'
end

答案 1 :(得分:1)

这是目前更新的一个。经过测试,工作正常

require 'rubygems'
require 'mechanize' 
require 'hpricot'

agent = Mechanize.new 
agent.user_agent_alias = 'Linux Firefox' 
page = agent.get('http://google.com/') 
google_form = page.form('f') google_form.q = 'your search'

page = agent.submit(google_form)

 page.links.each do |link|
if link.href.to_s =~/url.q/
        str=link.href.to_s
        strList=str.split(%r{=|&}) 
        url=strList[1]
        # if You need cached url's then just remove this condition and simply use URL's 
        if ! url.include? "webcache"
            puts url
        end
     end
  end 

只需创建一个新数组并将url推送到数组。

答案 2 :(得分:0)

现在不行,我想这可能是因为Google最近在搜索结果和网址中更改了HTML。

答案 3 :(得分:0)

如今,上面的答案不再适用了。我们发布了自己的宝石,易于使用,并允许自定义位置:

query = GoogleSearchResults.new q: "coffee", location: "Portand"
hash_results = query.get_hash

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