我正在使用Nokogiri解析播客的RSS源。我正在尝试获取包含该剧集链接的特定数据,因此我使用Nokogiri来解析RSS提要的XML响应。
相关位如下:
<item>
<title>An awesome title!</title>
...
<link>
http://www.foobar.com/episodes/1
</link>
</item>
Nokogiri似乎很难抓住<link>
标签;我可以将<item>
标记作为Nokogiri::Node
对象获取,我可以使用node.css('title').text
获取标题,但是当我使用node.css('link').text
尝试相同时,我得到一个空白字符串。
我尝试调用node.children.to_a
来检查此节点中的所有子节点,我注意到一些奇怪的事情:<link>
标记内的文本被解析为一个单独的子节点:
[0] = {Nokogiri::XML::Element} <title>An awesome title!</title>\n
[1] = {Nokogiri::XML::Element} <link>
[2] = {Nokogiri::XML::Text} http://www.foobar.com/episodes/1\n
有没有办法可以帮助Nokogiri正确解析这个多行标签,以便我可以抓住里面的文字?
UPDATE:以下是我遇到问题时正在执行的确切代码。
require 'open-uri'
doc = Nokogiri::HTML(open('https://rss.acast.com/abroadinjapan')) # Returns Nokogiri::HTML::Document
node = doc.css('//item').first # Returns Nokogiri::XML::Element
node.css('title').text # Returns "Abroad in Japan: Two weeks more in Japan!"
node.css('link').text # Returns ""
node.css('link').inner_text # Also returns "" - saw this elsewhere and thought I'd try it
node.children.to_a # Result, parsed by RubyMine for readability:
result = Array (14 elements)
[0] = {Nokogiri::XML::Element} <title>Abroad in Japan: Two weeks more in Japan!</title>\n
[1] = {Nokogiri::XML::Element} <subtitle>Chris and Pete return and they've planned out a very different route through Northern Japan.&nbsp;\n\n\nOur Google Map can be found here:&nbsp;\ngoo.gl/3t4t3q&nbsp;\n\n\nGet in touch:&nbsp;abroadinjapanpodcast@gmail.com&nbsp;\nMore Abr...</subtitle>
[2] = {Nokogiri::XML::Element} <summary></summary>
[3] = {Nokogiri::XML::Element} <guid ispermalink="false"></guid>
[4] = {Nokogiri::XML::Element} <pubdate>Wed, 16 May 2018 21:00:00 GMT</pubdate>
[5] = {Nokogiri::XML::Element} <duration>01:00:00</duration>
[6] = {Nokogiri::XML::Element} <keywords></keywords>
[7] = {Nokogiri::XML::Element} <explicit>no</explicit>
[8] = {Nokogiri::XML::Element} <episodetype>full</episodetype>
[9] = {Nokogiri::XML::Element} <image href="https://imagecdn.acast.com/image?h=1500&w=1500&source=https%3A%2F%2Fmediacdn.acast.com%2Fassets%2Fcb30d29f-7342-46f0-a649-12f1b4e601f7%2Fcover-image-jgyt2ecc-japan.jpg"></image>
[10] = {Nokogiri::XML::Element} <description>Chris and Pete return and they've planned out a very different route through Northern Japan. <p><br></p>\n<p>Our Google Map can be found here: </p>\n<p><a href="https://foobar.com/zqWZss9GSF" target="_blank">goo.gl/3t4t3q </a></p>\n<p><br></p>\n<p>Get in touch: <a href="mailto:abroadinjapanpodcast@gmail.com" target="_blank">abroadinjapanpodcast@gmail.com</a> </p>\n<p>More Abroad In Japan shows available below, do subscribe, rate and review us on iTunes, and please tell your friends! </p>\n<p><br></p>\n<p><a href="http://www.radiostakhanov.com/abroadinjapan/" target="_blank">http://www.radiostakhanov.com/abroadinjapan/</a></p>]]></description>
[11] = {Nokogiri::XML::Element} <link>
[12] = {Nokogiri::XML::Text} https://www.acast.com/abroadinjapan/abroadinjapan-twoweeksmoreinjapan-\n
[13] = {Nokogiri::XML::Element} <enclosure url="https://media.acast.com/abroadinjapan/abroadinjapan-twoweeksmoreinjapan-/media.mp3" length="28806528" type="audio/mpeg"></enclosure>
注意:上面的其中一个网址使用的是网址缩写器,但是我不喜欢它,因此我将其替换为foobar.com
。
答案 0 :(得分:3)
修复比你想象的要简单得多。 RSS提要不是有效的HTML,但它适用于XML:
doc = Nokogiri::XML(open('...'))
Ruby也有module named RSS,它可能更适合这样的事情:
require 'rss'
doc = RSS::Parser.parse(open('...'))
doc.items.first.link
=> "https://...."