A search spider i…

转自http://blog.dharanasoft.com/2012/03/19/a-search-spider-in-ruby-using-capybara-webkit/

Posted: March 19, 2012 Author: Deepak Prasanna | Filed under: Uncategorized |Leave a comment

When I first looked at Nokogiri, it was a redefining moment(atleast for me!) on how to screen scrap. Recently I found my love with cucumber and capybara-webkit. For newbies to capybara-webkit, it is a capybara driver which simulates a webkit browser for running tests. Perks? You get a simulated browser running in a headless mode, it supports javascript and its bloody fast! For more info, please checkout a previous article on how to get started. I was extremely bored this weekend, and all of a sudden an idea was born. I created a simple search spider using capybara-webkit which would fetch search results from google. And here is how I did it.

require 'ruby gems' 
require 'capybara' 
require 'capybara/dsl' 
require 'capybara-webkit' 
Capybara.run_server = false 
Capybara.current_driver = :webkit 
Capybara.app_host = "http://www.google.com/"  
module Spider 
 class Google 
 include Capybara::DSL 
 def search 
 visit('/') 
 fill_in "q", :with => ARGV[0] || "I love Ruby!" 
 click_button "Google Search" 
 all("li.g h3").each do |h3| 
 a = h3.find("a") 
 puts "#{h3.text}  =>  #{a[:href]}" 
 end 
 end 
 end 
end 
 spider = Spider::Google.new 
spider.search
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值