【发布时间】:2011-08-15 14:23:26
【问题描述】:
假设我在文档中有这个:
<entry>
<link rel="replies" type="application/atom+xml" href="http://www.url.com/feeds/1/comments/default" title="Comments"/>
<link rel="alternate" type="text/html" href="http://www.url.com/a_blog_post.html" title="A Blog Post"/>
</entry>
<entry>
<link rel="replies" type="application/atom+xml" href="http://www.url.com/feeds/2/comments/default" title="Comments"/>
<link rel="alternate" type="text/html" href="http://www.url.com/another_blog_post.html" title="Another Blog Post"/>
</entry>
我正在尝试使用 Nokogiri 来提取每个博客文章的 url,但我显然做错了(我是编程新手,无法理解 nokogiri)
这是我所拥有的:
require 'nokogiri'
require 'open-uri'
def get_posts(url)
posts = []
doc = Nokogiri::HTML(open(url))
doc.css('entry.alternate').each do |e|
puts e['href']
posts << e['href']
end
return posts
end
puts "Enter feed url:"
url = gets.chomp
posts = get_posts(url)
puts posts.to_s
任何帮助都会很棒!我开始这个小东西是为了更好地学习编程,但我被困住了。我目前的输出是[]
【问题讨论】: