Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: html2xml.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts an HTML file (.html) to an XML file (.xml).
- It utilizes BeautifulSoup to parse the HTML content and writes the XML representation to the specified XML file.
- Requirements:
- - Python 3.x
- - BeautifulSoup library (install using: pip install beautifulsoup4)
- Usage:
- 1. Save this script as 'html2xml.py'.
- 2. Ensure your HTML file ('example.html') is in the same directory as the script.
- 3. Install the BeautifulSoup library using the command: 'pip install beautifulsoup4'
- 4. Run the script.
- 5. The converted XML file ('html2xml.xml') will be generated in the same directory.
- Note: Adjust the 'html_filename' and 'xml_filename' variables in the script as needed.
- """
- from bs4 import BeautifulSoup
- def html_to_xml(html_filename, xml_filename):
- with open(html_filename, 'r') as htmlfile, open(xml_filename, 'w') as xmlfile:
- soup = BeautifulSoup(htmlfile, 'html.parser')
- xmlfile.write(str(soup))
- if __name__ == "__main__":
- html_filename = 'example.html'
- xml_filename = 'html2xml.xml'
- html_to_xml(html_filename, xml_filename)
- print(f"Converted '{html_filename}' to '{xml_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement