如何在所有标签中使用get_text()但在BeautifulSoup(Python)中没有一些特殊标签

时间:2017-03-21 16:17:00

标签: python beautifulsoup tags gettext except

所以我有例子

<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

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

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

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