-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_html.py
More file actions
executable file
·33 lines (28 loc) · 874 Bytes
/
Copy pathparse_html.py
File metadata and controls
executable file
·33 lines (28 loc) · 874 Bytes
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
from bs4 import BeautifulSoup
from bs4.element import Comment
import urllib.request
import os
def tag_visible(element):
if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
return False
if isinstance(element, Comment):
return False
return True
def text_from_html(body):
soup = BeautifulSoup(body, 'html.parser')
texts = soup.findAll(text=True)
visible_texts = filter(tag_visible, texts)
return u"\n ".join(t.strip() for t in visible_texts)
def get_text(html_file):
#html_file="/Users/rparikh/Downloads/Takeout/Keep/SuperValu.html"
b = os.path.basename(html_file)
n = b[:-5]+".txt"
f = open(html_file)
html= f.read()
f.close()
txt = text_from_html(html)
nf = os.path.dirname(html_file)+"/"+n
f = open(nf, "w")
f.write(txt)
f.close()
return nf