BeautifulSoup4 | 对于 BS4 初学者来说,从没有 ID 的表中快速、简单地收集文本信息的最简单方法是什么?

森沃克斯

我正在尝试创建一个从 Wowpedia 收集信息的网络抓取程序。(一个wikia风格的网站)

我现在主要关心的是有意义地能够简单地,所以我可以记住/参考未来,因为我真的很挣扎,当没有可靠的 ID 引用时,从表格中收集信息。最好,我也希望在不过多依赖单个 CSS 选择器的情况下执行此操作,但如果需要它们,我会很乐意提供帮助。

作为一个例子,我将提供一个我输入的简单代码片段来展示我将如何开始解决这个问题。

# Troll Grabber Version 0.1

from bs4 import BeautifulSoup
import requests

url = "https://wowpedia.fandom.com/wiki/Zul%27jin"
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")


bio = soup.find("table", class_="infobox darktable")
print(bio)

这段代码确实会抓取我想要的表格,并列出其中的所有内容。不过,我挣扎的事情是从中获取有意义的信息。

<table cellspacing="3" class="infobox darktable" style="border-spacing:3px;width:22em;width:21.5em;"><tbody><tr class="above-header"><th colspan="2" style="text-align:center;font-size:125%;font-weight:bold;padding:5px;"><span style="display:table;width:100%;border-collapse:collapse;border-spacing:0;"><span style="display:table-row;"><span style="display:table-cell;vertical-align:middle;padding-right:5px;width:1px;"><span class=""><a href="/wiki/Faction" title="Neutral"><img alt="Neutral" data-file-height="32" data-file-width="18" decoding="async" height="32" src="https://static.wikia.nocookie.net/wowpedia/images/1/19/Neutral_32.png/revision/latest/scale-to-width-down/18?cb=20110620212507" width="18"/></a></span></span><span style="display:table-cell;vertical-align:middle;padding-right:2px;">Zul'jin</span></span></span></th></tr><tr><td colspan="2" style="text-align:center;">
<a class="image" href="/wiki/File:Zul%27jinWallpaperCrop1.PNG" title="Image of Zul'jin"><img alt="Image of Zul'jin" data-file-height="653" data-file-width="469" decoding="async" height="278" src="https://static.wikia.nocookie.net/wowpedia/images/7/7d/Zul%27jinWallpaperCrop1.PNG/revision/latest/scale-to-width-down/200?cb=20071117013555" width="200"/></a></td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Title</th><td>
Warlord of Zul'Aman,<sup class="reference" id="cite_ref-UVG140_1-0"><a href="#cite_note-UVG140-1">[1]</a></sup> <a href="/wiki/Chieftain" title="Chieftain">Chieftain</a> of the Amani<sup class="reference" id="cite_ref-UVG140_1-1"><a href="#cite_note-UVG140-1">[1]</a></sup></td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Gender</th><td>
Male</td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Race</th><td>
<a href="/wiki/Forest_troll" title="Forest troll">Forest troll</a> (Undead)</td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Class</th><td>
<a href="/wiki/Warrior" title="Warrior">Warrior</a><sup class="reference" id="cite_ref-2"><a href="#cite_note-2">[2]</a></sup></td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Reaction</th><td>
<a href="/wiki/Alliance" title="Alliance"><span class="rep-friendly" title="Friendly">Alliance</span></a> <a href="/wiki/Horde" title="Horde"><span class="rep-friendly" title="Friendly">Horde</span></a></td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Former affiliation(s)</th><td>
<a href="/wiki/Amani_tribe" title="Amani tribe">Amani tribe</a>, <a href="/wiki/Old_Horde" title="Old Horde">Old Horde</a>, <a href="/wiki/Revantusk_tribe" title="Revantusk tribe">Revantusk tribe</a><sup class="reference" id="cite_ref-3"><a href="#cite_note-3">[3]</a></sup></td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Former occupation(s)</th><td>
<a href="/wiki/Axethrower" title="Axethrower">Axe thrower</a>, Ruler of the Amani tribe and <a href="/wiki/Zul%27Aman" title="Zul'Aman">Zul'Aman</a></td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Location</th><td>
<a href="#Locations">Various</a></td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Status</th><td>
Deceased<sup class="reference" id="cite_ref-4"><a href="#cite_note-4">[4]</a></sup></td></tr></tbody></table>

所有这些都是用 HTML 元素、引号、属性等编写的真正的满口内容。我想在其中隐藏信息。在绝望或更简单的输出时,我使用了长序列 str.replace() 和类似的东西来剥离所有这些 HTML。虽然我可以使用诸如 regex 之类的东西,但我想知道有没有更好的方法来收集这些信息,而无需将剪刀放在纸上。

而不是作为比赛输出,例如:

<a href="/wiki/Forest_troll" title="Forest troll">Forest troll</a> (Undead)</td></tr><tr><th scope="row" style="text-align:left;background-color:transparent;">Class</th><td>

更像这样的输出:

Race: Forest Troll (Undead)

对于这个项目,我想针对此表中的所有值实现与此类似的输出,但我对如何实现这一点一无所知。

我知道这绝对是我可以学习的东西,如果我真的开始阅读文档,我在编码时总是打开(以及其他网站)。但是像收集信息而不收集所有 HTML 信息这样简单的事情让我望而却步。像 get_text() 这样的命令似乎是朝着正确方向迈出的一步,直到我意识到它们不接受类参数并且似乎旨在收集页面上的每一位文本。

对于那些愿意花时间帮助我指教的人,我提前感谢你!

孟德尔

您可以这样做:

  1. 由于所有脚注都在 class 下reference,您可以从soupusing.decompose()方法中删除它们(类似于我之前的回答在这里)。

  2. 标题位于th标签下。发现。

  3. 其他数据在 a 下,td然后是先前找到的th查找调用.next_sibling的方法th


import requests
from bs4 import BeautifulSoup


URL = "https://wowpedia.fandom.com/wiki/Zul%27jin"

soup = BeautifulSoup(requests.get(URL).content, "html.parser")
table = soup.find("table", class_="infobox darktable")

# Remove all the footnotes from the table using the `decompose()` method
for tag in table.find_all(class_="reference"):
    tag.decompose()

for th in table.find_all("th")[1:]:  # <-- Using `[1:]` since we don't want the image
    print(f"{th.text}:  {th.next_sibling.get_text(strip=True)}")

输出:

Title:  Warlord of Zul'Aman,Chieftainof the Amani
Gender:  Male
Race:  Forest troll(Undead)
Class:  Warrior
Reaction:  AllianceHorde
Former affiliation(s):  Amani tribe,Old Horde,Revantusk tribe
Former occupation(s):  Axe thrower, Ruler of the Amani tribe andZul'Aman
Location:  Various
Status:  Deceased

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章