我正在做以下 Nokogiri 教程:http ://hunterpowers.com/data-scraping-and-more-with-ruby-nokogiri-sinatra-and-heroku/
所以我试图在终端中启动这个脚本:
require 'nokogiri'
require 'open-uri'
url = "http://www.930.com/concerts/#/930/"
data = Nokogiri::HTML(open(url))
# Here is where we use the new method to create an object that holds all the
# concert listings. Think of it as an array that we can loop through. It's
# not an array, but it does respond very similarly.
concerts = data.css('.concert_listing')
concerts.each do |concert|
# name of the show
puts concert.at_css('.event').text
# date of the show
puts concert.at_css('.date').text
# time of the show
puts concert.at_css('.doors').text
# show price or sold out
# Remember, when a show is sold out, there is no div with the selector .price
# What we are doing here is setting price = to that selector. We then test
# to see whether it is nil or not which let's us know if the show is SOLD OUT.
price = concert.at_css('.price')
if !price.nil?
puts price.text
else
puts "SOLD OUT"
end
# blank line to make results prettier
puts ""
end
和$ ruby interesting.rb
但什么也没发生:
alex@alex-K43U:~/rails/nokogiri$ rubyinteresting.rb
alex@alex-K43U:~/rails/nokogiri$
我以前用 Rails 做所有事情,所以现在从一个空文件夹开始对我来说似乎有点混乱。
如何在此文件夹中安装 gem 以及如何正确启动脚本?