我想使用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
这样的东西应该可以工作:
@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
这是目前更新的版本。测试和工作的良好
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推送到数组即可。
现在不起作用,我想这可能是因为谷歌最近更改了搜索结果和URL中的HTML。
如今,上面的答案已经不起作用了。我们发布了自己的宝石,易于使用,并允许自定义位置:
query = GoogleSearchResults.new q: "coffee", location: "Portand"
hash_results = query.get_hash
存储库:https://github.com/serpapi/google-search-results-ruby