我看了很多图,但是这个似乎已经让我到达了现在的位置
控制器
def index
require 'nokogiri'
doc = Nokogiri::XML(open("http://sports.yahoo.com/top/rss.xml"))
@links = doc.xpath('//item').map do |i|
{'title' => i.xpath('title'), 'link' => i.xpath('link'), 'description' => i.xpath('description')}
end
end
视图
<ul>
<%= debug @links.each.first %>
</ul>
调试输出
{"title"=>[#]>],"description"=>[#]>], "link"=>[#]>]}
我需要的是循环链接数组并访问带有标题和链接的哈希,但我不需要知道该怎么做。
I looked at a lot of tuts but this one seems to have got me where I am
controller
def index
require 'nokogiri'
doc = Nokogiri::XML(open("http://sports.yahoo.com/top/rss.xml"))
@links = doc.xpath('//item').map do |i|
{'title' => i.xpath('title'), 'link' => i.xpath('link'), 'description' => i.xpath('description')}
end
end
view
<ul>
<%= debug @links.each.first %>
</ul>
debug out put
{"title"=>[#<Nokogiri::XML::Element:0x8199ce34 name="title" children=[#<Nokogiri::XML::Text:0x8199c6f0 "Kolb to get start for Eagles vs. Falcons (AP)">]>], "description"=>[#<Nokogiri::XML::Element:0x8199b660 name="description" children=[#<Nokogiri::XML::Text:0x8199a594 "Kevin Kolb will make his second straight start in place of the injured Michael Vick when the Philadelphia Eagles host Atlanta on Sunday. Eagles coach Andy Reid says Vick practiced Friday for the first time since sustaining a rib cartilage injury on Oct. 3. There's a chance Vick will be the backup quarterback against his former team.">]>], "link"=>[#<Nokogiri::XML::Element:0x81999f40 name="link" children=[#<Nokogiri::XML::Text:0x81999b58 "http://us.rd.yahoo.com/sports/rss/top/SIG=11npql9k5/*http%3A//sports.yahoo.com/nfl/news?slug=ap-eagles-qbs">]>]}
What I need is to loop over the links array and access the hash with the title and link, but I don't know how to do that.
发布评论
评论(1)
如果我理解正确,您只需要从 xpath 节点获取内部文本即可。
…………
If I'm understanding correctly, you just need to get the inner text from the xpath nodes.
.......