Using BeautifulSoup to modify HTML

后端 未结 1 1259
温柔的废话
温柔的废话 2021-01-15 13:59

I want to use Beautifulsoup to modify a whole div of a HTML. I was trying to modify the HTML, however the console output has the modifications, but the actual .

相关标签:
1条回答
  • 2021-01-15 15:01

    Two things:

    1. You need to add some code to write the output from BeautifulSoup back to a file.
    2. You should use replace_with() to make changes to the HTML. By converting to a string, you were just modifying a textual copy.

    This can be done as follows:

    from bs4 import BeautifulSoup
    import os
    
    base = os.path.dirname(os.path.abspath(__file__))
    html = open(os.path.join(base, 'example.html'))
    soup = BeautifulSoup(html, 'html.parser')
    
    for i in soup.find('div', {"id":None}).findChildren():
        i.replace_with('##')
    
    with open("example_modified.html", "wb") as f_output:
        f_output.write(soup.prettify("utf-8"))  
    
    0 讨论(0)
提交回复
热议问题