0

我正在做以下 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$ ruby​​interesting.rb

alex@alex-K43U:~/rails/nokogiri$

我以前用 Rails 做所有事情,所以现在从一个空文件夹开始对我来说似乎有点混乱。

如何在此文件夹中安装 gem 以及如何正确启动脚本?

4

2 回答 2

1

在我看来很正常!你确定这条线:

concerts = data.css('.concert_listing')

导致concerts其中有任何可枚举的东西?你有没有试过这个?

puts concerts
于 2013-08-06T22:10:36.630 回答
1

如果您访问该站点,并弹出浏览器控制台并检查该页面,您会看到他们更改了 Concert 的 css 类,因此.concert_listing不再存在。
分析网站,看看你可以获取什么,以及如何使用 Nokogiri 获取它。

于 2013-08-06T22:12:22.000 回答