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

问题描述 投票:2回答:4

我想获得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
ruby-on-rails ruby nokogiri mechanize
4个回答
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
投票

这是现在更新的。经过测试,工作正常

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推送到数组。


0
投票

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


0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.