【问题标题】:How to get_text() in all tags but without some special tags in BeautifulSoup (Python)如何在所有标签中获取文本()但在 BeautifulSoup(Python)中没有一些特殊标签
【发布时间】:2017-08-13 10:44:41
【问题描述】:

所以我有例子

<body>
<div>I have a code 
    <img href"http://....jpg" />
    <p>title image</p>
    this so cool
</div>
</body>

那么我希望 OUTput 看起来像:

    I have a code 
    <img href"http://....jpg" />
    title image
    this so cool

非常感谢您的帮助

【问题讨论】:

  • &lt;img href"http://....jpg" /&gt; 不是文本。所以你不能使用get_text()来获取它们。
  • 好的,谢谢兄弟,你能帮帮我吗
  • 您需要什么帮助?
  • 你能在上面的例子中给出一些解决方案吗,谢谢
  • 你需要div标签内的所有东西?

标签: python beautifulsoup tags gettext except


【解决方案1】:

您可以使用返回div 内容列表的.contents。然后你相​​应地处理结果。

soup = BeautifulSoup(html, "html.parser")    
soup.find("div").contents

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-06
    • 2017-09-21
    • 1970-01-01
    • 2015-08-29
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多