Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: html2csv.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts an HTML file (.html) to a CSV file (.csv).
- It uses BeautifulSoup to parse the HTML content and extracts table rows, writing them to a CSV file.
- Requirements:
- - Python 3.x
- - BeautifulSoup library (install using: pip install beautifulsoup4)
- Usage:
- 1. Save this script as 'html2csv.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 CSV file ('html2csv.csv') will be generated in the same directory.
- Note: Adjust the 'html_filename' and 'csv_filename' variables in the script as needed.
- """
- import csv
- from bs4 import BeautifulSoup
- def html_to_csv(html_filename, csv_filename):
- with open(html_filename, 'r') as htmlfile, open(csv_filename, 'w', newline='') as csvfile:
- csvwriter = csv.writer(csvfile)
- soup = BeautifulSoup(htmlfile, 'html.parser')
- for row in soup.find_all('tr'):
- csvwriter.writerow([col.get_text(strip=True) for col in row.find_all(['td', 'th'])])
- if __name__ == "__main__":
- html_filename = 'example.html'
- csv_filename = 'html2csv.csv'
- html_to_csv(html_filename, csv_filename)
- print(f"Converted '{html_filename}' to '{csv_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement