-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] add back
parse
with output to JSON and CSV + example notebook
- Loading branch information
Showing
6 changed files
with
756 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from bs4 import BeautifulSoup | ||
import io | ||
import csv | ||
|
||
def run_convert(result, output_format): | ||
if output_format == "JSON": | ||
converter = _html_table_string_to_json | ||
elif output_format == "CSV": | ||
converter = _html_table_to_csv | ||
else: | ||
return result | ||
|
||
return [converter(table) for table in result] | ||
|
||
def _html_table_string_to_json(html_string: str): | ||
soup = BeautifulSoup(html_string, 'html.parser') | ||
table = soup.find('table') | ||
|
||
if not table: | ||
raise ValueError('No table found in the provided HTML string.') | ||
|
||
rows = table.find_all('tr') | ||
headers = [cell.get_text(strip=True) for cell in rows[0].find_all(['th', 'td'])] | ||
|
||
result = [] | ||
|
||
for row in rows[1:]: | ||
cells = row.find_all('td') | ||
row_object = {} | ||
|
||
for header, cell in zip(headers, cells): | ||
row_object[header] = cell.get_text(strip=True) | ||
|
||
result.append(row_object) | ||
|
||
return result | ||
|
||
def _html_table_to_csv(html_string: str) -> str: | ||
soup = BeautifulSoup(html_string, 'html.parser') | ||
table = soup.find('table') | ||
|
||
if not table: | ||
raise ValueError('No table found in the provided HTML string.') | ||
|
||
rows = table.find_all('tr') | ||
output = io.StringIO() | ||
csv_writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL) | ||
|
||
for row in rows: | ||
cells = row.find_all(['th', 'td']) | ||
csv_writer.writerow([cell.get_text(strip=True) for cell in cells]) | ||
|
||
return output.getvalue() |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters