-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlToMarkdown.py
More file actions
54 lines (42 loc) · 1.28 KB
/
Copy pathhtmlToMarkdown.py
File metadata and controls
54 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from markdownify import markdownify as md
from requests import get
import re
import sys
if len(sys.argv) == 1:
sys.exit(f"Error\nUsage: {sys.argv[0]} file.html num_table")
# Get params
try:
url = sys.argv[1]
table = int(sys.argv[2])
except ValueError:
sys.exit(f"{sys.argv[2]} can't cast to Int")
except IndexError:
sys.exit(f"Error\nUsage: {sys.argv[0]} file.html num_table")
# Get html
print("Getting html code ...")
try:
html_response = get(url)
html = html_response.content.decode("utf-8")
html = html.splitlines()
except requests.exceptions.MissingSchema as error:
print(str(error))
# Get table's tags
print("Extracting table's tags index")
bgs = [idx for idx, line in enumerate(html, start=1)
if re.search(r"<table[^>]*>", line)]
ends = [idx for idx, line in enumerate(html, start=1)
if re.search(r"</table>", line)]
# Get table's code
print(f"Getting table {table}'s code")
try:
bg = bgs[table - 1]
end = ends[table - 1]
except IndexError:
sys.exit("Error: number of table out of bounds")
table_code = [html[line] for line in range(bg, end)]
table_code = " ".join(table_code)
# Transforming
print("HTML -> MD")
with open("table.md", "w") as file:
file.write(md(table_code))
print("Process finished with exit code 0")