如何使用BeautifulSoup删除嵌套标签中的内容?

时间:2014-02-13 14:44:46

标签: python html nested beautifulsoup

如何使用BeautifulSoup删除嵌套代码中的内容?这些帖子反过来检索嵌套代码中的内容:How to get contents of nested tag using BeautifulSoupBeautifulSoup: How do I extract all the <li>s from a list of <ul>s that contains some nested <ul>s?

我尝试了.text,但它只删除了标记

>>> from bs4 import BeautifulSoup as bs
>>> html = "<foo>Something something <bar> blah blah</bar> something</foo>"
>>> bs(html).find_all('foo')[0]
<foo>Something something <bar> blah blah</bar> something else</foo>
>>> bs(html).find_all('foo')[0].text
u'Something something  blah blah something else'

期望的输出:

  

其他东西

3 个答案:

答案 0 :(得分:3)

您可以查看有关儿童的bs4.element.NavigableString

from bs4 import BeautifulSoup as bs
import bs4
html = "<foo>Something something <bar> blah blah</bar> something <bar2>GONE!</bar2> else</foo>"
def get_only_text(elem):
    for item in elem.children:
        if isinstance(item,bs4.element.NavigableString):
            yield item

print ''.join(get_only_text(bs(html).find_all('foo')[0]))

输出;

Something something  something  else

答案 1 :(得分:1)

例如

body = bs(html)
for tag in body.find_all('bar'):
    tag.replace_with('')

答案 2 :(得分:0)

这是我的简单方法soup.body.clear()soup.tag.clear()

假设您要清除<table></table>中的内容并添加一个新的pandas数据框;稍后,您可以使用此清除方法轻松地在网页的html文件中更新表格,而不是flask / django:

    import pandas as pd
    import bs4

我想将120万行.csv转换为DataFrame,然后转换为HTML表,         然后将其添加到我网页的html语法中。后来我想轻松         只要通过切换变量

来更新csv,就可以更新数据
    bizcsv = read_csv("business.csv")
    dframe = pd.DataFrame(bizcsv)
    dfhtml = dframe.to_html #convert DataFrame to table, HTML format
    dfhtml_update = dfhtml_html.strip('<table border="1" class="dataframe">, </table>')
    """use dfhtml_update later to update your table without the <table> tags,
    the <table> is easy for BS to select & clear!"""

    #A small function to unescape (&lt; to <) the tags back into HTML format
    def unescape(s):
        s = s.replace("&lt;", "<")
        s = s.replace("&gt;", ">")
        # this has to be last:
        s = s.replace("&amp;", "&")
        return s

    with open("page.html") as page:  #return to here when updating
        txt = page.read()
        soup = bs4.BeautifulSoup(txt, features="lxml")
        soup.body.append(dfhtml) #adds table to <body>
        with open("page.html", "w") as outf:
            outf.write(unescape(str(soup))) #writes to page.html

    """lets say you want to make seamless table updates to your 
    webpage instead of using flask or django x_x; return to with open function"""
    soup.table.clear()  #clears everything in <table></table>
    soup.table.append(dfhtml_update)
    with open("page.html", "w") as outf:
        outf.write(unescape(str(soup))) 

我是一个新手,但是经过无数次搜索,我只是结合了文档中的一些基础知识……有点ated肿,但实际上正在处理数十亿个数据单元。这对我有用