-
Notifications
You must be signed in to change notification settings - Fork 5
/
parse.py
175 lines (162 loc) · 5.51 KB
/
parse.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#! encoding: utf-8
import bs4
from bs4 import BeautifulSoup
import os
import sys
import traceback
import time
import html2text
reload(sys)
sys.setdefaultencoding('utf-8')
def _find_title_from_tag(soup, tagname, default):
try:
content = soup.find_all(tagname, limit=10)
for i in content:
text = i.text.encode("utf-8")
if ":" in text or "年" in text or "TOC" in text or "---" in text:
continue
if len(text) < 64 and len(text) > 6:
title = text
title = title.replace(" ", "_")
title = title.replace("\xc2\xa0", "")
title = title.replace("\n", "_")
title = title.replace("/", "_")
title = title.replace("-", "_")
title = title.replace(">", "_")
title = title.replace(",", "")
title = title.replace("!", "")
title = title.replace("<", "_")
title = title.replace("=", "")
title = title.replace("#", "")
title = title.replace(" ", "")
title = title.replace("__", "_")
title = title.replace("__", "_")
title = title.replace("__", "_")
if len(title) == 0:
continue
if title == "无标题":
continue
title = title[1:] if title[0] == "_" else title
title = os.path.dirname(default) +"/" + title
return title
except:
pass
return None
def _find_title_from_body(soup, default):
try:
for i in soup.body.find_all(True):
text = i.text.encode("utf-8")
if ":" in text or "年" in text or "TOC" in text or "---" in text:
continue
if len(text) < 64 and len(text) > 6:
title = text
title = title.replace(" ", "_")
title = title.replace("\xc2\xa0", "")
title = title.replace("\n", "_")
title = title.replace("/", "_")
title = title.replace("-", "_")
title = title.replace(">", "_")
title = title.replace(",", "")
title = title.replace("!", "")
title = title.replace("<", "_")
title = title.replace("=", "")
title = title.replace("#", "")
title = title.replace(" ", "")
title = title.replace("__", "_")
title = title.replace("__", "_")
title = title.replace("__", "_")
if len(title) == 0:
continue
if title == "无标题":
continue
title = title[1:] if title[0] == "_" else title
title = os.path.dirname(default) +"/" + title
return title
except:
pass
return None
def _get_title(soup, default):
r = _find_title_from_tag(soup, ["title"], default)
if r:
return r
r = _find_title_from_tag(soup, ["h3", "h4", "h1", "h2", "h5"], default)
if r:
return r
r = _find_title_from_body(soup, default)
if r:
return r
"""
r = _find_title_from_tag(soup, ["p"], default)
if r:
return r
r = _find_title_from_tag(soup, ["span"], default)
if r:
return r
r = _find_title_from_tag(soup, ["div"], default)
if r:
return r
"""
return default.replace(".html", "")
def parse_html(path):
try:
html = open(path, "r").read()
h2t = html2text.HTML2Text(bodywidth=0)
h2t.inline_links = True
h2t.ignore_links = True
h2t.ignore_images = True
h2t.single_line_break = True
soup = BeautifulSoup(html,'html.parser',from_encoding='utf-8')
title = _get_title(soup, path)
"""
# 提取所有文本, 每个tag是一行
for i in soup.body.find_all(True):
print i.get_text()
"""
dstfilename = "%s.md" % title
dstfile = open(dstfilename, "w+")
print "Parse HTML", path, "->", "%s.md"%title
#dstfile.write(h2t.handle(html))
item = soup.body
for i in item:
dstfile.write(i.get_text())
dstfile.write("\n")
dstfile.close()
except Exception as e:
print "Error %s %s"%(e, path)
exc_type, exc_value, exc_traceback_obj = sys.exc_info()
print "exc_type: %s" % exc_type
print "exc_value: %s" % exc_value
#print "exc_traceback_obj: %s" % exc_traceback_obj
traceback.print_tb(exc_traceback_obj)
def main():
if len(sys.argv) > 1:
filelist = sys.argv[1]
block = None
if len(sys.argv) > 2:
block = sys.argv[2]
fd = open(filelist, "r")
while True:
ln = fd.readline()
if not ln:
break
if block:
if hash(ln) % 4 != int(block):
continue
ln = ln.replace("\n", "")
if ln == "":
continue
parse_html(ln)
fd.close()
if __name__ == "__main__":
"""
find ./notes/extract_* -type f > .filelist
python parse.py .filelist
"""
try:
main()
except Exception as e:
exc_type, exc_value, exc_traceback_obj = sys.exc_info()
print "exc_type: %s" % exc_type
print "exc_value: %s" % exc_value
#print "exc_traceback_obj: %s" % exc_traceback_obj
traceback.print_tb(exc_traceback_obj)