gpt4 book ai didi

Ruby 每个循环都没有为每个元素完成

转载 作者:太空宇宙 更新时间:2023-11-03 18:30:42 26 4
gpt4 key购买 nike

以下代码:

# 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

正在 View 中输出:

iTunes StoreThis American LifeNPR: Wait Wait... Don't Tell Me! PodcastStuff You Should KnowFreakonomics RadioNPR: Fresh Air PodcastNPR: Car Talk PodcastWNYC's RadiolabDespicable MePearls Before Swine Animated CartoonsThe Moth PodcastAPM: A Prairie Home Companion's News from Lake WobegonHarry Potter Years 1-5 PodcastAce On The HouseTakers - Takers Featurette: Executing the Heist - The Making of TakersNPR: Planet Money PodcastStuff You Missed in History ClassThe Dave Ramsey ShowBook ReviewGlobal NewsVampires Suck ClipsNPR: Science Friday PodcastOther Guys Crash and BurnBack to WorkNPR: All Songs Considered PodcastNPR: Tiny Desk 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/despicable-me/id399247154?uo=2&uo=2http://itunes.apple.com/us/podcast/pearls-before-swine-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-companions/id215352157?uo=2&uo=2http://itunes.apple.com/us/podcast/harry-potter-years-1-5-podcast/id322144752?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-considered-podcast/id79687345?uo=2&uo=2http://itunes.apple.com/us/podcast/npr-tiny-desk-concerts-podcast/id362115318?uo=2&uo=2

您可以看到它在继续访问 url 之前获取了所有元素的名称。我希望它先评估每个元素的名称,然后是 url 等,然后再转到下一个元素。我做错了什么。

谢谢。

最佳答案

导致此问题的原因有很多。首先,当您在 each 循环中使用 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 以便它只返回当前节点。我还将每个更改为映射,以便它将变量分配给一个数组,其中包含 block 返回的所有值。我删除了 return 的调用,因为它没有必要。

因此这将导致包含名称和 url 的数组数组

关于Ruby 每个循环都没有为每个元素完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4778391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com