【问题标题】:Beautifulsoup how to parse h2 and underlying h3Beautifulsoup 如何解析 h2 和底层 h3
【发布时间】:2018-07-13 03:05:57
【问题描述】:

我是解析 lxml 的新手(从今天下午开始),所以请耐心等待。我正在尝试使用带有漂亮汤的 lxml 标记来解析带有超级英雄数据的 csv。 我想在两个 h2 标签 Powers and Abilities & Weapons and Equipment 中获取文本

soup.find_all("h2")
 >> [<h2><strong>Origin</strong></h2>,
 <h2><strong>Creation</strong></h2>,
 <h2><strong>Character Evolution</strong></h2>,
 <h2><strong>Major Story Arcs</strong></h2>,
 <h2><strong>Powers and Abilities</strong></h2>,
 <h2><strong>Weapons and Equipment</strong></h2>,
 <h2><strong>Character Profile</strong></h2>,
 <h2><strong>Alternate Realities </strong></h2>,
 <h2><strong>Other Media</strong></h2>,
 <h2>Merchandising</h2>,
 <h2><strong>Depiction and the Iconic Costume</strong></h2>,
 <h2>Popular Recognition</h2>]

当我查看 h2 的下一个兄弟(文本在 h3 标签中)的武器和装备时,我只得到一个东西回来(应该更多)。当我将 header.text 更改为 Weapons and Equipment 时,我没有得到任何结果。

    for header in soup.find_all('h2'):
        if header.text == "Weapons and Equipment":
            nextNode = header.nextSibling
            print(nextNode.text)
            if nextNode is None:
                break
>> Lasso of Truth

力量和能力 + 武器确实出现在 findall('h3') 结果中(连同其他我不想要的东西)

soup.find_all("h3", text=True)
>> [<h3>Challenge of the Gods</h3>,
 <h3>First clash with Circe</h3>,
 <h3>The New 52</h3>,
 <h3>Meeting Zeus Other Children</h3>,
 <h3>Meeting First Born</h3>,
 <h3><strong>Superhuman Strength</strong></h3>,
 <h3><strong>Superhuman Speed</strong></h3>,
 <h3><strong>Invulnerability/Durability</strong></h3>,
 <h3><strong>Flight</strong></h3>,
 <h3><strong>Healing Factor</strong></h3>,
 <h3><strong>Divine Wisdom</strong></h3>,
 <h3><strong>Super Stamina/Agility</strong></h3>,
 <h3><strong>Great Beauty </strong></h3>,
 <h3><strong>Enhanced Sense</strong></h3>,
 <h3><strong>Other Assorted Divine Powers</strong></h3>,
 <h3><strong>God of War Powers</strong></h3>,
 <h3><strong>Martial Combat</strong></h3>,
 <h3><strong>Lasso of Truth</strong></h3>,
 <h3><strong>Bracelets of Victory</strong></h3>,
 <h3><b>Royal Tiara</b></h3>,
 <h3><strong>The Invisible Plane</strong></h3>,
 <h3><strong>Battle Armour</strong></h3>,
 <h3><strong>Martial Weapons</strong></h3>,
 <h3><strong>Magical Sword</strong></h3>,
 <h3><strong>Sandals of Hermes</strong></h3>,
 <h3><strong>Gauntlet of Atlas</strong></h3>,
 <h3><strong>Earrings</strong></h3>,
 <h3><strong>Power Rings</strong></h3>,
 <h3>War suits (or uniform)</h3>,
 <h3>The Dark Knight Strikes Again</h3>]

我整个下午都在阅读文档,但没有找到可以帮助我的示例。我真的不知道我应该如何获得这些物品。帮助和解释将不胜感激。

数据的sn-p

    h2>Powers And Abilities </h2><br /><br /><ul class="plain-list">
<li>Reality Manipulation - Wanda possesses the ability to manipulate reality         
based on how hard she "wonders". The full extent of this ability is unknown, 
but it is known that she once wondered two of her enemies into non-existence 
during a battle in Las Vegas, Nevada.</li> <li>Psionic Abilities -  The full 
extent of Wanda's psionic abilities is currently unknown, but has been shown 
to include Mental Telepathy, Telekinesis, and Empathy.   <br /></li> 
<li>Superhuman Intelligence - Wanda possesses superhuman intelligence, 
including perfect memory, and data analysis. According to Woo-Z Winks, this 
may cause a problem in battle, as Wanda allows herself to battle purely on 
instinct, while her mind becomes lost in some deep philosophical point.  <br 
/></li> <li>Superhuman 
Strength<br /></li> <li>Superhuman Speed<br /></li> <li>Superhuman Agility<br />
</li> <li>Superhuman Dexterity<br /></li> <li>Superhuman Reflexes<br /></li> 
<li>Superhuman Senses</li></ul><p> <br /> </p> <h2>Paraphenalia </h2>

     <h3><ins>Weapons</ins></h3><p><strong>Lasso of truth</strong></p><p>The 
original lasso, still in existence in</p><p><strong>Harmony and 
Charity</strong></p><p>Wonder woman bracelets, posses intelligence and are 
programed with battle strategies.</p><p><strong>Invisible 
spacecraft</strong></p><p>Wonder Woman carries a invisible 

【问题讨论】:

  • 请添加您尝试处理的数据的最小示例,以便我们可以看到它的结构以及您用来处理它的任何代码。请阅读minimal reproducible example
  • 很难举个例子。数据来自comicvine api(我假设他们使用了网络爬虫,因为布局看起来像网页)。我只在变量汤中加载了一个 csv。所以那里也没有帮助
  • 请不要张贴图片,复制粘贴,然后像代码一样格式化。
  • 像代码一样添加它^^

标签: python python-3.x beautifulsoup lxml


【解决方案1】:

我不认为这种“导航到兄弟姐妹”方法最适合这种情况。您最好尝试使用更好的选择器。

通过使用soup.select 方法,您可以使用CSS 选择器来匹配多个标签:

for headers in soup.select('h2,h3'):
    if "THIS" in header.text or "THAT" in header.text:
        print(header.text)

CSS 选择器功能强大且灵活。建议大家多研究一下:https://www.w3schools.com/cssref/css_selectors.asp

【讨论】:

  • 嗯,这不会返回任何东西。如果我在 for 循环之后添加一个打印语句,它会打印标题。但是在 header.text == 之后我什么也没得到
  • 由于文本在 元素内,您可以使用子字符串比较。检查更新
  • 不幸的是,我无法通过子字符串比较看到更新。我可以看到您编辑了原始响应,但 OP 中的 for 循环不起作用。
  • 我可以例如用 h3 代替 strong ,当我打印 headers.text 时,我看到了文本。对于 header.text 中的 if "Weapons and Equipment" 或 header.text 中的 "Powers and Abilities",我一无所获:print(header.text)
  • 这可能与python中的字符串比较有关...我会在这里做一些假设。
猜你喜欢
  • 2015-10-25
  • 1970-01-01
  • 2012-04-07
  • 2021-12-16
  • 2019-11-04
  • 2020-09-20
  • 2020-10-02
  • 2013-08-11
  • 1970-01-01
相关资源
最近更新 更多