我有一个小的 Sinatra 应用程序:
应用程序.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
显示.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 中的链接中提取文本(例如 Live on NBA ...):
并将它们显示在我的模板中。
(现在,我只知道如何从带有类和 ID 的 html 标签中提取文本)。