-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtml2text.py
284 lines (239 loc) · 9.2 KB
/
html2text.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/python2.5
"""
File: html2text.py
Copyright (C) 2008 Chris Spencer (chrisspen at gmail dot com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""
import os, sys, htmllib, formatter, StringIO, re, HTMLParser, htmlentitydefs
import socket, httplib, urllib2, time
try:
import tidy
except ImportError, e:
print "You need to install the Python wrapper for TidyLib."
raise
def unescapeHTMLEntities(text):
"""Removes HTML or XML character references
and entities from a text string.
keep &, >, < in the source code.
from Fredrik Lundh
http://effbot.org/zone/re-sub.htm#unescape-html
"""
def fixup(m):
text = m.group(0)
if text[:2] == "&#":
# character reference
try:
if text[:3] == "&#x":
return unichr(int(text[3:-1], 16))
else:
return unichr(int(text[2:-1]))
except ValueError:
pass
else:
# named entity
try:
text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
except KeyError:
pass
return text # leave as is
return re.sub("&#?\w+;", fixup, text)
class TextExtractor(HTMLParser.HTMLParser):
"""
Attempts to extract the main body of text from an HTML document.
This is a messy task, and certain assumptions about the story text must be made:
The story text:
1. Is the largest block of text in the document.
2. Sections all exist at the same relative depth.
"""
dom = []
path = [0]
pathBlur = 5
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self._ignore = False
self._ignorePath = None
self._lasttag = None
self._depth = 0
self.depthText = {} # path:text
self.counting = 0
self.lastN = 0
def handle_starttag(self, tag, attrs):
ignore0 = self._ignore
tag = tag.lower()
if tag in ('script','style','option','ul','li','legend','object','noscript','label'): # 'h1','h2','h3','h4','h5','h6',
self._ignore = True
attrd = dict(attrs)
self._lasttag = tag.lower()
self._depth += 1
self.path += [self.lastN]
self.lastN = 0
# Ignore footer garbage.
if 'id' in attrd and 'footer' in attrd['id'].lower():
self._ignore = True
elif 'id' in attrd and 'copyright' in attrd['id'].lower():
self._ignore = True
elif 'class' in attrd and 'footer' in attrd['class'].lower():
self.counting = max(self.counting,1)
self._ignore = True
elif 'class' in attrd and 'copyright' in attrd['class'].lower():
self._ignore = True
# If we just started ignoring, then remember the initial path
# so we can later know when to start un-ignoring again.
if self._ignore and not ignore0:
self._ignorePath = tuple(self.path)
def handle_startendtag(self, tag, attrs):
pass
def handle_endtag(self, tag):
if self._ignore and tuple(self.path) == self._ignorePath:
self._ignore = False
self._depth -= 1
self.lastN = self.path.pop()
self.lastN += 1
def handle_data(self, data, entity=False):
if len(data) > 0 and not self._ignore:
# Skip blocks of text beginning with 'copyright', which usually
# indicates a copyright notice.
if data.strip().lower().startswith('copyright') and not self._ignore:
self._ignore = True
self._ignorePath = tuple(self.path)
return
if data:
rpath = tuple(self.path[:-self.pathBlur])
self.depthText.setdefault(rpath, [])
self.depthText[rpath] += [data]
# Allow one more layer below, to include
# text inside <i></i> or <b></b> tags.
# Unfortuantely, this will include a lot of crap
# in the page's header and footer, so we'll
# prefix this text with '#' and strip these out later.
rpath2 = tuple(self.path[:-self.pathBlur-1])
self.depthText.setdefault(rpath2, [])
self.depthText[rpath2] += ['#'+data]
def handle_charref(self, name):
if name.isdigit():
text = unescapeHTMLEntities('&#'+name+';')
else:
text = unescapeHTMLEntities('&'+name+';')
self.handle_data(text, entity=True)
def handle_entityref(self, name):
self.handle_charref(name)
def get_plaintext(self):
maxLen,maxPath,maxText,maxTextList = 0,None,'',[]
for path,textList in self.depthText.iteritems():
# Strip off header segments, prefixed with a '#'.
start = True
text = []
for t in textList:
if len(t.strip()):
if t.startswith('#') and start:
continue
start = False
text.append(t)
# Strip off footer segments, prefixed with a '#'.
start = True
textList = reversed(text)
text = []
for t in textList:
if len(t.strip()):
if t.startswith('#') and start:
continue
start = False
text.append(t)
text = reversed(text)
text = ''.join(text).replace('#','')
try:
text = text.replace(u'\xa0',' ')
except UnicodeDecodeError:
pass
text = text.replace(u'\u2019',"'")
text = re.sub("[\\n\\s]+", " ", text).strip() # Compress whitespace.
#text = re.sub("[\W]+", " ", text).strip() # Compress whitespace.
maxLen,maxPath,maxText,maxTextList = max((maxLen,maxPath,maxText,maxTextList), (len(text),path,text,textList))
return maxText
def error(self,msg):
# ignore all errors
pass
class HTMLParserNoFootNote(htmllib.HTMLParser):
"""
Ignores link footnotes, image tags, and other useless things.
"""
textPattern = None
path = [0]
def handle_starttag(self, tag, attrs, *args):
time.sleep(0.5)
self.path += [0]
if tag == 'script':
pass
def handle_endtag(self, tag, *args):
self.path.pop()
self.path[-1] += 1
if tag == 'script':
pass
def anchor_end(self):
if self.anchor:
#self.handle_data("[%d]" % len(self.anchorlist))
self.anchor = None
def handle_image(self, src, alt, *args):
pass
def handle_data(self, data):
if self.textPattern:
data = ' '.join(self.textPattern.findall(data))
htmllib.HTMLParser.handle_data(self, data)
def extractFromHTML(html):
"""
Extracts text from HTML content.
"""
# create memory file
file = StringIO.StringIO()
# convert html to text
f = formatter.AbstractFormatter(formatter.DumbWriter(file))
p = TextExtractor()
try:
p.feed(html)
except (AttributeError, AssertionError):
return None
p.close()
text = p.get_plaintext()
text = re.sub("\s[\(\),;\.\?\!](?=\s)", " ", text).strip() # Remove stand-alone punctuation.
text = re.sub("[\n\s]+", " ", text).strip() # Compress whitespace.
text = re.sub("\-{2,}", "", text).strip() # Remove consequetive dashes.
text = re.sub("\.{2,}", "", text).strip() # Remove consequetive periods.
return text
def tidyHTML(dirtyHTML):
"""
Runs an arbitrary HTML string through Tidy.
"""
file = StringIO.StringIO()
options = dict(output_xhtml=1, add_xml_decl=1, indent=1, tidy_mark=1)
html = tidy.parseString(dirtyHTML, **options)
html.write(file)
html = file.getvalue()
return html
def extractFromURL(url):
"""
Extracts text from a URL.
"""
try:
stream = urllib2.urlopen(url)
html = stream.read()
stream.close()
except (ValueError, IOError, httplib.InvalidURL, httplib.BadStatusLine,
socket.timeout, socket.error):
return None
# Convert content to XHTML.
html = tidyHTML(html)
extracted_content = extractFromHTML(html)
try:
return str(extracted_content)
except (UnicodeEncodeError, UnicodeDecodeError):
return None