blob: e17977413c0696d50a20a1e4d9c29f19b4a46778 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#!/usr/bin/env python3
import sys
from bs4 import BeautifulSoup
import csv
def main(file_):
with open(file_, "r") as f:
file_contents = f.read()
soup = BeautifulSoup(file_contents)
table = soup.select_one("table")
headers = [th.text.encode("utf-8") for th in table.select("tr th")]
with open("out.csv", "w") as f:
wr = csv.writer(f)
wr.writerow(headers)
wr.writerows([[td.text.encode("utf-8") for td in row.find_all("td")] for row in table.select("tr + tr")])
if __name__ == '__main__':
main(sys.argv[1])
|