Advertisement
Python253

html2xml

Mar 13th, 2024
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: html2xml.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts an HTML file (.html) to an XML file (.xml).
  10. It utilizes BeautifulSoup to parse the HTML content and writes the XML representation to the specified XML file.
  11.  
  12. Requirements:
  13. - Python 3.x
  14. - BeautifulSoup library (install using: pip install beautifulsoup4)
  15.  
  16. Usage:
  17. 1. Save this script as 'html2xml.py'.
  18. 2. Ensure your HTML file ('example.html') is in the same directory as the script.
  19. 3. Install the BeautifulSoup library using the command: 'pip install beautifulsoup4'
  20. 4. Run the script.
  21. 5. The converted XML file ('html2xml.xml') will be generated in the same directory.
  22.  
  23. Note: Adjust the 'html_filename' and 'xml_filename' variables in the script as needed.
  24. """
  25. from bs4 import BeautifulSoup
  26.  
  27. def html_to_xml(html_filename, xml_filename):
  28.     with open(html_filename, 'r') as htmlfile, open(xml_filename, 'w') as xmlfile:
  29.         soup = BeautifulSoup(htmlfile, 'html.parser')
  30.         xmlfile.write(str(soup))
  31.  
  32. if __name__ == "__main__":
  33.     html_filename = 'example.html'
  34.     xml_filename = 'html2xml.xml'
  35.     html_to_xml(html_filename, xml_filename)
  36.     print(f"Converted '{html_filename}' to '{xml_filename}'.")
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement