使用类从div的子元素中提取文本?

时间:2013-08-06 22:40:18

标签: ruby nokogiri

我有一个小型的Sinatra应用程序:

app.rb:

get '/' do
  # the first two lines are lifted directly from our previous script
  url = "http://www.nba.com/"
  data = Nokogiri::HTML(open(url))

  # this line has only be adjusted slightly with the inclusion of an ampersand
  # before concerts.  This creates an instance variable that can be referenced
  # in our display logic (view).
  @headlines = data.css('#nbaAssistSkip')
  @top_stories = data.css('#nbaAssistSkip')

  # this tells sinatra to render the Embedded Ruby template /views/shows.erb
  erb :shows
end

show.erb:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
  <meta charset="UTF-8">
  <title>Nokogiri App</title>
</head>
<body>
  <div>
  <h2><%= @headlines %></h2>
  <p><%= @top_stories %></p>
  </div>
</body>
</html>

我是Nokogiri的新手,我想知道如何从.nbaBreakingNews div中的链接中提取文本(例如,直播NBA ......):

enter image description here

并在我的模板中显示它们。

(现在,我只知道如何从带有类和ID的html标签中提取文本)。

1 个答案:

答案 0 :(得分:1)

这些部分中的a元素将是:

data.css('.nbaBreakingNewscv a')

这意味着任何a元素都来自具有类nbaBreakingNewscv的元素。要显示您要执行的a元素的文本:

data.css('.nbaBreakingNewscv a').each do |a|
  puts a.text
end