以下代码:
# fetch the top 300 podcasts from itunes
itunes_top_300 = Nokogiri.HTML(open("http://itunes.apple.com/us/rss/toppodcasts/limit=25/xml"))
# parse the returned xml with nokogiri
itunes_top_300.xpath('//feed/entry').each do |entry|
name = entry.xpath("//name").text
url = entry.xpath("//link/@href").text
category = entry.xpath("//category/@term").text
hosts = entry.xpath("//artist").text
summary = entry.xpath("//summary").text
artwork = entry.xpath("//image[@height='170']").text
return name + url
end
正在视图中输出:
iTunes StoreThis American LifeNPR:等等别告诉我PodcastStuff You Should Known Freakonomics RadioNPR:Fresh Air PodcastNPR:Car Talk PodcastWnyc's RadioAbdable Mepearls Before Pine动画片The Moth PodcastAPM:A Prairie Home Companion's News from Lake Wobegonhary Potter Years 1-5 Podcasace on the Housetakers-Takers Featurette:执行抢劫-Takers制作:星球金钱播客历史课上你错过的东西戴夫·拉姆齐展示本评论全球新闻吸血鬼吸人clipsnpr:science friday播客其他人崩溃并恢复工作npr:all songs concepts concerts podcasthttp://itunes.apple.com/webobjects/mzstore.woa/wa/viewtop?id=38&popId=3http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppodcasts/limit=25/xml?cc=ushttp://itunes.apple.com/us/podcast/this-american-life/id201671138?uo=2&uo=2http://itunes.apple.com/us/podcast/npr-wait-wait-dont-tell-me/id121493804?uo=2&uo=2http://itunes.apple.com/us/podcast/stuff-you-should-know/id278981407?uo=2&uo=2http://itunes.apple.com/us/podcast/freakonomics-radio/id354668519?uo=2&uo=2http://itunes.apple.com/us/podcast/npr-fresh-air-podcast/id214089682?uo=2&uo=2http://itunes.apple.com/us/podcast/npr-car-talk-podcast/id253191823?uo=2&uo=2http://itunes.apple.com/us/podcast/wnycs-radiolab/id152249110?uo=2&uo=2http://itunes.apple.com/us/podcast/desibable-me/id399247154?uo=2&uo=2http://itunes.apple.com/us/podcast/pearls-before-pine-animated/id409382502?uo=2&uo=2http://itunes.apple.com/us/podcast/the-moth-podcast/id275699983?uo=2&uo=2http://itunes.apple.com/us/podcast/apm-a-prairie-home-components/id215352157?uo=2&uo=2http://itunes.apple.com/us/podcast/harry-potter-years-1-5-podcast/id32214752?uo=2&uo=2http://itunes.apple.com/us/podcast/ace-on-the-house/id414294132?uo=2&uo=2http://itunes.apple.com/us/podcast/takers-takers-featurette-executing/id412910974?uo=2&uo=2http://itunes.apple.com/us/podcast/npr-planet-money-podcast/id290783428?uo=2&uo=2http://itunes.apple.com/us/podcast/stuff-you-missed-in-history/id283605519?uo=2&uo=2http://itunes.apple.com/us/podcast/the-dave-ramsey-show/id77001367?uo=2&uo=2http://itunes.apple.com/us/podcast/book-review/id120315179?uo=2&uo=2http://itunes.apple.com/us/podcast/global-news/id135067274?uo=2&uo=2http://itunes.apple.com/us/podcast/vampires-suck-clips/id405404825?uo=2&uo=2http://itunes.apple.com/us/podcast/npr-science-friday-podcast/id73329284?uo=2&uo=2http://itunes.apple.com/us/podcast/other-guys-crash-and-burn/id407622041?uo=2&uo=2http://itunes.apple.com/us/podcast/back-to-work/id415535037?uo=2&uo=2http://itunes.apple.com/us/podcast/npr-all-songs-consided-podcast/id79687345?uo=2&uo=2http://itunes.apple.com/us/podcast/npr-tiny-desk-concerts-podcast/id362115318?uo=2&uo=2
在转到url之前,您可以看到它正在获取所有元素的名称在转到下一个元素之前,我希望它先计算每个元素的名称,然后计算url等我做错什么了。
谢谢。
最佳答案
造成这个问题的原因有很多首先,当您在每个循环中使用return时,实际上是在破坏它,因此它只迭代一次,而不是25次。
其次,您可能不会注意到它只运行一次,因为在xpath中使用//name时,它会返回所有名称。
或许你可以这样做:
# Returns top 25 since the url includes limit=25
itunes_top_25 = Nokogiri.XML(open("http://itunes.apple.com/us/rss/toppodcasts/limit=25/xml"))
names_and_urls = itunes_top_25.xpath('//feed/entry').map do |entry|
name = entry.xpath("./name").text
url = entry.xpath("./link/@href").text
category = entry.xpath("./category/@term").text
hosts = entry.xpath("./artist").text
summary = entry.xpath("./summary").text
artwork = entry.xpath("./image[@height='170']").text
[name, url]
end
我将//name改为./name,这样它只返回当前节点我还将每个变量都更改为map,以便它将变量分配给包含块返回的所有值的数组。我取消了回电话,因为没必要。
因此这将产生一个包含名称和url的数组数组
关于ruby - Ruby的每个循环还没有完成每个元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4778391/