该脚本用于将html table脚本转换为csv文件。
from bs4 import BeautifulSoup
import csv
html = """
<YOUR HTML TABLE HERE>
"""
soup = BeautifulSoup(html, 'html.parser')
# Extract headers
headers = [th.get_text(strip=True) for th in soup.find_all('th')]
# Extract rows
rows = []
for tr in soup.find_all('tr')[1:]: # skip the header row
cells = tr.find_all('td')
row = [cell.get_text(strip=True) for cell in cells]
rows.append(row)
# Write to CSV
with open('output1.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(rows)
print("CSV file created successfully.")