Insert html string into BeautifulSoup object

后端 未结 2 2113
暖寄归人
暖寄归人 2021-01-05 02:29

I am trying to insert an html string into a BeautifulSoup object. If I insert it directly, bs4 sanitizes the html. If take the html string and create a soup from it, and ins

2条回答
  •  情歌与酒
    2021-01-05 02:55

    Simplest way, if you already have an html string, is to insert another BeautifulSoup object.

    from bs4 import BeautifulSoup
    
    doc = '''
    
    test1
    ''' soup = BeautifulSoup(doc, 'html.parser') soup.div.append(BeautifulSoup('
    insert1
    ', 'html.parser')) print soup.prettify()

    Output:

    test1
    insert1

    Update 1

    How about this? Idea is to use BeautifulSoup to generate the right AST node (span tag). Looks like this avoids the "None" problem.

    import bs4
    from bs4 import BeautifulSoup
    
    mainSoup = BeautifulSoup("""
    
        
    """, 'html.parser') extraSoup = BeautifulSoup('', 'html.parser') tag = mainSoup.find(class_='first') tag.insert(1, extraSoup.span) print mainSoup.find(class_='second')

    Output:

提交回复
热议问题