我正在为学校的一个项目工作,我在网站的HTML中找到正确的CSS选择器以拉入我正在寻找的数据时遇到了问题。这也是我第一次使用web抓取&我也是Ruby的新手,所以如果这是一个愚蠢的问题,我道歉。
我已经成功地解析了第一组数据(尽管我相信有更好的方法可以做到这一点,我的方法正在工作,但即使是对此的反馈也是受欢迎的):
该网站为platinumgod.co.uk,以供参考。
我为第一部分抓取的HTML如下所示(以及作为示例列出的第一项):
<div class="repentanceitems-container">
<h2>
"Repentance Items "
<span class="rep-item-ttl">(169)</span>
</h2>
<li class="textbox" data-tid="42.5" data-cid="42" data-sid="263">
<a
<div onclick class="item reb-itm-new re-itm263"></div>
<span>
<p class="item-title">Clear Rune</p>
<p class="r-itemid">ItemID: 263</p>
<p class="pickup">"Rune mimic"</p>
<p class="quality">Quality: 2</p>
<p>"When used, copies the effect of the Rune or Soul stone you are holding (like the Blank Card)"</p>
<p>Drops a random rune on the floor when picked up</p>
<p>The recharge time of this item depends on the Rune/Soul Stone held:</p>
<p>1 room: Soul of Lazarus</p>
<p>2 rooms: Rune of Ansuz, Rune of Berkano, Rune of Hagalaz, Soul of Cain</p>
<p>3 rooms: Rune of Algiz, Blank Rune, Soul of Magdalene, Soul of Judas, Soul of ???, Soul of the Lost</p>
<p>4 rooms: Rune of Ehwaz, Rune of Perthro, Black Rune, Soul of Isaac, Soul of Eve, Soul of Eden, Soul of the Forgotten, Soul of Jacob and Esau</p>
<p>6 rooms: Rune of Dagaz, Soul of Samson, Soul of Azazel, Soul of Apollyon, Soul of Bethany</p>
<p>12 rooms: Rune of Jera, Soul of Lilith, Soul of the Keeper</p>
<ul>
<p>Type: Active</p>
<p>Recharge time: Varies</p>
<p>Item Pool: Secret Room, Crane Game</p>
</ul>
<p class="tags">* Secret Room</p>
</span>
</a>
</li>这只是“悔改项目”类别中的一个项目的示例,所以这是我的代码,用于解析该类别中每个项目的所有信息:
# Repentance Items
repentance_items = []
html.at(".repentanceitems-container").css("li.textbox").each do |item |
item_name = item.css("a span p.item-title").text
item_id = item.css("a span p.r-itemid").text.sub(/^ItemID: /, "")
pickup_text = item.css("a span p.pickup").text.gsub("\"", "")
quality = item.css("a span p.quality").text.sub(/^Quality: /, "")
use = item.css(".quality ~ p:not(.tags)").map { |row| row.text }
item_type = item.css("a span ul")
item.css("a span ul").each.map do |child|
item_type = child.css("p")[0].text.sub(/^Type: /, "")
if child.css("p")[1].text.match "Recharge time"
recharge_time = child.css("p")[1].text.sub(/^Recharge time: /, "")
item_pool = child.css("p")[2].text.sub(/^Item Pool: /, "").gsub(/,\s*$/m, "").split(", ")
else
recharge_time = "N/A"
item_pool = child.css("p")[1].text.sub(/^Item Pool: /, "").gsub(/,\s*$/m, "").split(", ")
end
repentance_items << {name: item_name, item_id: item_id, pickup_text: pickup_text, quality: quality, use: use, item_type: item_type, recharge_time: recharge_time, item_pool: item_pool}
end
end我面临的问题是,当我试图抓取下一个类别时,我不确定CSS选择器应该是什么才能获得这些信息,因为许多相同的类被用在了悔改项目HTML中,所以我只是得到了之前做过的相同的项目。小装饰品的HTML如下所示(以及作为示例列出的第一项):
<div class="repentanceitems-container">
<h2>
"Repentance Trinkets "
<span class="a-item-ttl">(61)</span>
</h2>
<li class="textbox" data-tid="1000" data-cid="804" data-sid="10129">
<a
<div onclick class="item rep-item rep-trink rep-junxx129"></div>
<span>
<p class="item-title">Jawbreaker</p>
<p class="r-itemid">TrinketID: 129</p>
<p class="pickup">"Don't chew on it"</p>
<p>Tears have a chance to become a tooth, dealing x3.2 damage, similar to Tough Love</p>
<p>The chance to fire a tooth with this trinket is affected by your Luck stat</p>
<p>At +0 luck you have ~12% chance for this effect to activate</p>
<p>At +9 luck every tear you fire will be a tooth</p>
<p class="tags">*, </p>
</span>
</a>
</li>为了只选择这些项目,我不确定从哪里开始。如果我使用在代码的第一部分中使用的相同选择器,那么很明显,它只是重新拉入悔过项&而不是小装饰品。
希望我已经解释得足够好了,但是请随时问我更多的问题&我会尽我最大的努力解释得更好。
提前感谢大家对我的帮助!
发布于 2021-09-30 07:24:32
也许你可以把你的第一行选择器分成两部分:一部分用于捕获容器,另一部分用于查找项。这可能看起来像这样(未测试):
repentance_items = []
repentance_trinklets = []
html.at(".repentanceitems-container").each do |container|
# Check to know in what category you are, so in which table to add the results, something like:
repentance_target = if container.css('h2').text =~ /items/i
repentance_items
else
repentance_trinklets
end
css("li.textbox").each do |item|
# your current logic
# affectation in the correct results array
repentance_target << ...
end
end最后,应该用正确的项填充这两个数组
这是一个有点草率,但我希望这有助于,让我知道如果有什么不清楚
https://stackoverflow.com/questions/69383949
复制相似问题