从谷歌搜索结果中获取链接

时间:2017-09-13 19:04:46

标签: ruby web-crawler mechanize

我有这个代码,它给了我任何谷歌搜索条目的搜索结果的标题。但是我想要“href”标记标记中的链接。示例代码是

<h3 class="r"><a href="https://www.lonelyplanet.com/india" onmousedown="return rwt(this,'','','','1','AFQjCNG5Z2TycA5Rni1X_vKY3Gt9BEVS4w','','0ahUKEwi-99jMpqXWAhUKO48KHdfqBcIQtwIIJzAA','','',event)" target="_blank">Lonely Planet India - India - Lonely Planet</a></h3>



require 'mechanize'
agent = Mechanize.new
page = agent.get("https://www.google.com/videohp")
search_form = page.form('f')
search_form.q = 'india'
page = agent.submit(search_form)
puts page.search('h3.r').map(&:text)

1 个答案:

答案 0 :(得分:0)

page.search('h3.r a').map{|a| a['href']}