How can I strip comment tags from HTML using BeautifulSoup?

后端 未结 4 791
暖寄归人
暖寄归人 2020-11-28 13:41

I have been playing with BeautifulSoup, which is great. My end goal is to try and just get the text from a page. I am just trying to get the text from the body, with a speci

4条回答
  •  野性不改
    2020-11-28 14:11

    If you are looking for solution in BeautifulSoup version 3 BS3 Docs - Comment

    soup = BeautifulSoup("""Hello! """)
    comment = soup.find(text=re.compile("if"))
    Comment=comment.__class__
    for element in soup(text=lambda text: isinstance(text, Comment)):
        element.extract()
    print soup.prettify()
    

提交回复
热议问题