0
我有一個小西納特拉應用:從提取一個div一類的子元素的文本?
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
專區內提取的鏈接的文本(例如住在NBA ...):
而且在我的模板顯示它們。
(現在,我只知道如何從HTML標籤處理類和ID中提取文本)。