我有一个包含引号的 Wordpress 博客的 XML 文件:
<item>
<title>Brothers Karamazov</title>
<content:encoded><![CDATA["I think that if the Devil doesn't exist and, consequently, man has created him, he has created him in his own image and likeness."]]></content:encoded>
<category domain="post_tag" nicename="dostoyevsky"><![CDATA[Dostoyevsky]]></category>
<category domain="post_tag" nicename="humanity"><![CDATA[humanity]]></category>
<category domain="category" nicename="quotes"><![CDATA[quotes]]></category>
<category domain="post_tag" nicename="the-devil"><![CDATA[the Devil]]></category>
</item>
我要提取的内容是标题、作者、内容和标签。到目前为止,这是我的代码:
require "rubygems"
require "nokogiri"
doc = Nokogiri::XML(File.open("/Users/charliekim/Downloads/quotesfromtheunderground.wordpress.2013-04-14.xml"))
doc.css("item").each do |item|
title = item.at_css("title").text
tag = item.at_xpath("category").text
content = item.at_xpath("content:encoded").text
#each post will later be pushed to an array, but I'm not worried about that yet, so for now....
puts "#{title} #{tag}"
end
我正在努力从每个item
. 我得到了类似的回报Brothers Karamazov Dostoyevsky
。我不担心它的格式,因为它只是一个测试,看看它是否正确地拾取东西。有人知道我该怎么做吗?
我还想制作大写 = Author 的标签,所以如果你知道怎么做,它也会有所帮助,尽管我还没有尝试过。
编辑:我将代码更改为:
doc.css("item").each do |item|
title = item.at_css("title").text
content = item.at_xpath("content:encoded").text
tag = item.at_xpath("category").each do |category|
category
end
puts "#{title}: #{tag}"
end
返回:
Brothers Karamazov: [#<Nokogiri::XML::Attr:0x80878518 name="domain" value="post_tag">, #<Nokogiri::XML::Attr:0x80878504 name="nicename" value="dostoyevsky">]
这似乎更易于管理。它搞砸了我从大写标签中获取作者的计划,但是,这没什么大不了的。我怎么能只拉第二个value
?