forked from netconstructor/python-readability
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadability.py
executable file
·343 lines (286 loc) · 11.3 KB
/
readability.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python
from BeautifulSoup import NavigableString
from page_parser import parse, get_title, get_body, Unparseable
import logging
import re
REGEXES = { 'unlikelyCandidatesRe': re.compile('combx|comment|disqus|foot|header|menu|meta|nav|rss|shoutbox|sidebar|sponsor',re.I),
'okMaybeItsACandidateRe': re.compile('and|article|body|column|main',re.I),
'positiveRe': re.compile('article|body|content|entry|hentry|page|pagination|post|text',re.I),
'negativeRe': re.compile('combx|comment|contact|foot|footer|footnote|link|media|meta|promo|related|scroll|shoutbox|sponsor|tags|widget',re.I),
'divToPElementsRe': re.compile('<(a|blockquote|dl|div|img|ol|p|pre|table|ul)',re.I),
'replaceBrsRe': re.compile('(<br[^>]*>[ \n\r\t]*){2,}',re.I),
'replaceFontsRe': re.compile('<(\/?)font[^>]*>',re.I),
'trimRe': re.compile('^\s+|\s+$/'),
'normalizeRe': re.compile('\s{2,}/'),
'killBreaksRe': re.compile('(<br\s*\/?>(\s| ?)*){1,}/'),
'videoRe': re.compile('http:\/\/(www\.)?(youtube|vimeo)\.com', re.I),
}
from collections import defaultdict
def describe(node):
if not hasattr(node, 'name'):
return "[text]"
return "%s#%s.%s" % (
node.name, node.get('id', ''), node.get('class',''))
def _text(node):
return " ".join(node.findAll(text=True))
class Document:
TEXT_LENGTH_THRESHOLD = 25
RETRY_LENGTH = 250
def __init__(self, input, notify=None, **options):
self.input = input
self.options = defaultdict(lambda: None)
for k, v in options.items():
self.options[k] = v
self.notify = notify or logging.info
self.html = None
def _html(self, force=False):
if force or self.html is None:
self.html = parse(self.input, self.options['url'], notify=self.notify)
return self.html
def content(self):
return get_body(self._html())
def title(self):
return get_title(self._html())
def summary(self):
try:
ruthless = True
while True:
self._html(True)
[i.extract() for i in self.tags(self.html, 'script', 'style')]
if ruthless: self.remove_unlikely_candidates()
self.transform_misused_divs_into_paragraphs()
candidates = self.score_paragraphs(self.options.get('min_text_length', self.TEXT_LENGTH_THRESHOLD))
best_candidate = self.select_best_candidate(candidates)
if best_candidate:
article = self.get_article(candidates, best_candidate)
else:
if ruthless:
ruthless = False
self.debug("ended up stripping too much - going for a safer parse")
# try again
continue
else:
article = self.html.find('body') or self.html
cleaned_article = self.sanitize(article, candidates)
of_acceptable_length = len(cleaned_article or '') >= (self.options['retry_length'] or self.RETRY_LENGTH)
if ruthless and not of_acceptable_length:
ruthless = False
continue # try again
else:
return cleaned_article
except StandardError, e:
logging.exception('error getting summary:')
raise Unparseable(str(e))
def get_article(self, candidates, best_candidate):
# Now that we have the top candidate, look through its siblings for content that might also be related.
# Things like preambles, content split by ads that we removed, etc.
sibling_score_threshold = max([10, best_candidate['content_score'] * 0.2])
output = parse("<div/>")
for sibling in best_candidate['elem'].parent.contents:
if isinstance(sibling, NavigableString): continue
append = False
if sibling is best_candidate['elem']:
append = True
sibling_key = HashableElement(sibling)
if sibling_key in candidates and candidates[sibling_key]['content_score'] >= sibling_score_threshold:
append = True
if sibling.name == "p":
link_density = self.get_link_density(sibling)
node_content = sibling.string or ""
node_length = len(node_content)
if node_length > 80 and link_density < 0.25:
append = True
elif node_length < 80 and link_density == 0 and re.search('\.( |$)', node_content):
append = True
if append:
output.append(sibling)
if not output: output.append(best_candidate)
return output
def select_best_candidate(self, candidates):
sorted_candidates = sorted(candidates.values(), key=lambda x: x['content_score'], reverse=True)
self.debug("Top 5 candidates:")
for candidate in sorted_candidates[:5]:
elem = candidate['elem']
self.debug("Candidate %s with score %s" % (describe(elem), candidate['content_score']))
if len(sorted_candidates) == 0:
return None
best_candidate = sorted_candidates[0]
self.debug("Best candidate %s with score %s" % (describe(best_candidate['elem']), best_candidate['content_score']))
return best_candidate
def get_link_density(self, elem):
link_length = len("".join([i.text or "" for i in elem.findAll("a")]))
text_length = len(_text(elem))
return float(link_length) / max(text_length, 1)
def score_paragraphs(self, min_text_length):
candidates = {}
elems = self.tags(self.html, "p","td")
for elem in elems:
parent_node = elem.parent
grand_parent_node = parent_node.parent
parent_key = HashableElement(parent_node)
grand_parent_key = HashableElement(grand_parent_node)
inner_text = _text(elem)
# If this paragraph is less than 25 characters, don't even count it.
if (not inner_text) or len(inner_text) < min_text_length:
continue
if parent_key not in candidates:
candidates[parent_key] = self.score_node(parent_node)
if grand_parent_node and grand_parent_key not in candidates:
candidates[grand_parent_key] = self.score_node(grand_parent_node)
content_score = 1
content_score += len(inner_text.split(','))
content_score += min([(len(inner_text) / 100), 3])
candidates[parent_key]['content_score'] += content_score
if grand_parent_node:
candidates[grand_parent_key]['content_score'] += content_score / 2.0
# Scale the final candidates score based on link density. Good content should have a
# relatively small link density (5% or less) and be mostly unaffected by this operation.
for elem, candidate in candidates.items():
candidate['content_score'] *= (1 - self.get_link_density(elem))
self.debug("candidate %s scored %s" % (describe(elem), candidate['content_score']))
return candidates
def class_weight(self, e):
weight = 0
if e.get('class', None):
if REGEXES['negativeRe'].search(e['class']):
weight -= 25
if REGEXES['positiveRe'].search(e['class']):
weight += 25
if e.get('id', None):
if REGEXES['negativeRe'].search(e['id']):
weight -= 25
if REGEXES['positiveRe'].search(e['id']):
weight += 25
return weight
def score_node(self, elem):
content_score = self.class_weight(elem)
name = elem.name.lower()
if name == "div":
content_score += 5
elif name == "blockquote":
content_score += 3
elif name == "form":
content_score -= 3
elif name == "th":
content_score -= 5
return { 'content_score': content_score, 'elem': elem }
def debug(self, *a):
if self.options['debug']:
logging.debug(*a)
def remove_unlikely_candidates(self):
for elem in self.html.findAll():
s = "%s%s" % (elem.get('class', ''), elem.get('id', ''))
if REGEXES['unlikelyCandidatesRe'].search(s) and (not REGEXES['okMaybeItsACandidateRe'].search(s)) and elem.name != 'body':
self.debug("Removing unlikely candidate - %s" % (s,))
elem.extract()
def transform_misused_divs_into_paragraphs(self):
for elem in self.html.findAll():
if elem.name.lower() == "div":
# transform <div>s that do not contain other block elements into <p>s
if REGEXES['divToPElementsRe'].search(''.join(map(unicode, elem.contents))):
self.debug("Altering div(#%s.%s) to p" % (elem.get('id', ''), elem.get('class', '')))
elem.name = "p"
def tags(self, node, *tag_names):
for tag_name in tag_names:
for e in node.findAll(tag_name):
yield e
def sanitize(self, node, candidates):
for header in self.tags(node, "h1", "h2", "h3", "h4", "h5", "h6"):
if self.class_weight(header) < 0 or self.get_link_density(header) > 0.33: header.extract()
for elem in self.tags(node, "form", "iframe"):
elem.extract()
# Conditionally clean <table>s, <ul>s, and <div>s
for el in self.tags(node, "table", "ul", "div"):
weight = self.class_weight(el)
el_key = HashableElement(el)
if el_key in candidates:
content_score = candidates[el_key]['content_score']
else:
content_score = 0
name = el.name
if weight + content_score < 0:
el.extract()
self.debug("Conditionally cleaned %s with weight %s and content score %s because score + content score was less than zero." %
(describe(el), weight, content_score))
elif len(_text(el).split(",")) < 10:
counts = {}
for kind in ['p', 'img', 'li', 'a', 'embed', 'input']:
counts[kind] = len(el.findAll(kind))
counts["li"] -= 100
content_length = len(_text(el)) # Count the text length excluding any surrounding whitespace
link_density = self.get_link_density(el)
to_remove = False
reason = ""
if counts["img"] > counts["p"]:
reason = "too many images"
to_remove = True
elif counts["li"] > counts["p"] and name != "ul" and name != "ol":
reason = "more <li>s than <p>s"
to_remove = True
elif counts["input"] > (counts["p"] / 3):
reason = "less than 3x <p>s than <input>s"
to_remove = True
elif content_length < (self.options.get('min_text_length', self.TEXT_LENGTH_THRESHOLD)) and (counts["img"] == 0 or counts["img"] > 2):
reason = "too short a content length without a single image"
to_remove = True
elif weight < 25 and link_density > 0.2:
reason = "too many links for its weight (#{weight})"
to_remove = True
elif weight >= 25 and link_density > 0.5:
reason = "too many links for its weight (#{weight})"
to_remove = True
elif (counts["embed"] == 1 and content_length < 75) or counts["embed"] > 1:
reason = "<embed>s with too short a content length, or too many <embed>s"
to_remove = True
if to_remove:
self.debug("Conditionally cleaned %s#%s.%s with weight %s and content score %s because it has %s." %
(el.name, el.get('id',''), el.get('class', ''), weight, content_score, reason))
el.extract()
for el in ([node] + node.findAll()):
if not (self.options['attributes']):
el.attrMap = {}
return unicode(node)
class HashableElement():
def __init__(self, node):
self.node = node
self._path = None
def _get_path(self):
if self._path is None:
reverse_path = []
node = self.node
while node:
node_id = (node.name, tuple(node.attrs), node.string)
reverse_path.append(node_id)
node = node.parent
self._path = tuple(reverse_path)
return self._path
path = property(_get_path)
def __hash__(self):
return hash(self.path)
def __eq__(self, other):
return self.path == other.path
def __getattr__(self, name):
return getattr(self.node, name)
def main():
import sys
from optparse import OptionParser
parser = OptionParser(usage="%prog: [options] [file]")
parser.add_option('-v', '--verbose', action='store_true')
parser.add_option('-u', '--url', help="use URL instead of a local file")
(options, args) = parser.parse_args()
if not (len(args) == 1 or options.url):
parser.print_help()
sys.exit(1)
logging.basicConfig(level=logging.DEBUG)
file = None
if options.url:
import urllib
file = urllib.urlopen(options.url)
else:
file = open(args[0])
try:
print Document(file.read(), debug=options.verbose).summary().encode('ascii','ignore')
finally:
file.close()
if __name__ == '__main__':
main()