[Python] Merging HTML files with python

Discussão em 'Python' iniciado por Stack, Abril 2, 2025.

  1. Stack

    Stack Membro Participativo

    import os
    from bs4 import BeautifulSoup

    output_doc = BeautifulSoup()
    output_doc.append(output_doc.new_tag("html"))
    output_doc.html.append(output_doc.new_tag("body"))

    for file in os.listdir(data_folder):
    if not file.lower().endswith('.html'):
    continue

    with open(file, 'r') as html_file:
    output_doc.body.extend(BeautifulSoup(html_file.read(), "html.parser").body)

    print(output_doc.prettify())


    I have 5 different html files (file1, file2, file3, file4 and file5) which I want to combine into a single file. upon merging using the code above, the combined html output has file1 content repeating 5 times before adding file 2 contents (which also repeated 5 times), before adding file 3 contents etc. What I was expecting is just a single file with 5 output and not 25 [​IMG]

    Continue reading...

Compartilhe esta Página