How to save back changes made to a HTML file using BeautifulSoup in Python?

前端 未结 1 950
孤城傲影
孤城傲影 2020-12-23 22:15

I have the script below, which modifies href attributes in an HTML file (in the future, it will be a list of HTML files in a directory). Using BeautifulSoup I m

相关标签:
1条回答
  • 2020-12-23 22:46
    newlink = link['href']
    # .. make replacements
    link['href'] = newlink # store it back
    

    Now print(soup.prettify()) will show changed links. To save the changes to a file:

    htmlDoc.close()
    
    html = soup.prettify("utf-8")
    with open("output.html", "wb") as file:
        file.write(html)
    

    To preserve original character encoding of the document, you could use soup.original_encoding instead of "utf-8". See Encodings.

    0 讨论(0)
提交回复
热议问题