-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadability.py
294 lines (239 loc) · 8.12 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
"""A Python port of arc90's Readability.js
http://lab.arc90.com/experiments/readability/
This is hacky, minimally tested, and not yet optimized for speed. But it gets
the job done. Much of it is a line-by-line translation of arc90's js code to
python. Caveat Utor.
example call:
import readability
text = readability.get_text(html)
title = readability.get_title(html)
Related:
* Nirmal Patel's readability python port: http://nirmalpatel.com/fcgi/hn.py
(less powerful but works decently)
* Bonus approach: http://stackoverflow.com/questions/2921237/is-there-anything-for-python-that-is-like-readability-js/2934186#2934186
TODO:
* clean up title processing
* get some external reviewers/cleanup
* look at readability site for more improvements
* get rid of unused stuff
* profile for speed
* more formally test on an assembled corpus.
"""
import re
import sys
from BeautifulSoup import BeautifulSoup, Comment
DEBUG = False
def dbg(text):
"""Poor-man's logging."""
if DEBUG:
sys.stderr.write('%s\n', text)
UNLIKELY_CANDIDATES_RE = re.compile(
'combx|comment|disqus|foot|header|menu|rss|shoutbox|sidebar|sponsor|ad-break')
OK_MAYBE_ITS_A_CANDIDATE_RE = re.compile('and|article|body|column|main')
POSITIVE_RE = re.compile(
'article|body|content|entry|hentry|page|pagination|post|text|blog')
NEGATIVE_RE = re.compile( 'combx|comment|contact|foot|footer|footnote|link|masthead|media|meta|promo|related|scroll|shoutbox|sponsor|tags|widget')
DIV_TO_P_ELEMENTS_RE = re.compile(
'<(a|blockquote|dl|div|img|ol|p|pre|table|ul)')
REPLACE_BRS_RE_2 = re.compile('<br */? *>[ \r\n]*<br */? *>')
# REPLACE_BRS_RE = re.compile('(<br[^>]*>[ \n\r\t]*){2,}')
# REPLACE_FONTS_RE = re.compile('<(\/?)font[^>]*>')
# TRIM_RE = re.compile('^\s+|\s+$')
# NORMALIZE_RE = re.compile('\s{2,}')
# KILL_BREAKS_RE = re.compile('(<br\s*\/?>(\s| ?)*){1,}')
# VIDEO_RE = re.compile('http:\/\/(www\.)?(youtube|vimeo)\.com')
# SKIP_FOOTNOTE_LINK_RE = re.compile(
# '^\s*(\[?[a-z0-9]{1,2}\]?|^|edit|citation needed)\s*$')
GOOD_NAMES = re.compile('div')
OK_NAMES = re.compile('pre|td|blockquote')
BAD_NAMES = re.compile('address|ol|ul|dl|dd|dt|li|form')
HORRIBLE_NAMES = re.compile('h1|h2|h3|h4|h5|h6|th')
def _textify(soup, html=False):
"""Grab the plaintext from a node."""
if not html:
retval = [t.strip(' ') for t in soup.findAll(text=True) if t.strip()]
retval = ''.join(retval)
retval = re.sub('[ \t]+', ' ', retval)
retval = re.sub('[\n\f\v]+', '\n', retval)
return retval.strip()
return soup.prettify()
def get_title(soup):
"""Get the title from a soup-parsed web page."""
try:
orig_title = soup.findAll('title')[0]
except:
orig_title = ''
title = orig_title
# TODO: take care of titles like foo-bar, foo|bar, and foo: bar
"""
if ' | ' in orig_title or ' - ' in orig_title:
title = re.sub('(.*)- .*', '\1', orig_title)
if title.count(' ') < 3:
title = re.sub('[^\|\-]*[\|\-](.*)', '\1', orig_title)
"""
if len(title) > 150 or len(title) < 15:
h1s = list(soup.findAll('h1'))
if len(h1s) == 1:
title = h1s[0]
return _textify(title)
def get_text(html):
"""Get the contentful text from a soup-parsed web page.
This is the meat of the script."""
html = re.sub(REPLACE_BRS_RE_2, "</p><p>", html)
try:
soup = BeautifulSoup(html)
except HTMLParser.HTMLParseError:
return ''
for node in get_bad_nodes(soup):
node.extract()
candidates = get_candidates(soup)
if not candidates:
return ''
best = None
for candidate in get_candidates(soup):
score = rank(candidate)
if not best or best.score < score:
best = candidate
candidates = sorted([[c.score, c] for c in candidates])
best = candidates[0][1]
# TODO: later, only do this if it doesn't hurt length too much.
strip_junk_tags(best)
return _textify(best)
def get_bad_nodes(soup):
"""Return the nodes that are bad. All script nodes, also cruddy ones."""
for node in soup.findAll('script'):
yield node
for node in soup.findAll('link', attrs={"type" : "text/css"}):
yield node
for node in soup.findAll('style'):
yield node
# Get rid of html comments.
for node in soup.findAll(text=lambda text:isinstance(text, Comment)):
yield node
#for node in soup.findAll('object'):
# yield node
for node in soup.findAll():
classid = node.get('id', '') + node.get('class', '')
if node.name == 'body':
continue
if UNLIKELY_CANDIDATES_RE.match(classid):
if not OK_MAYBE_ITS_A_CANDIDATE_RE.match(classid):
dbg('removing: %s, %s' % (node.name, classid))
yield node
def get_candidates(soup):
"""Get candidate nodes for ranking."""
candidates = []
for tag in ('p', 'td', 'pre'):
for n in soup.findAll(tag):
p = n.parent
if not p in candidates:
if len(_textify(n)) >= 25:
candidates.append(p)
# also look for divs where used inappropriately
# (as in, where they contain no other block level elements.)
"""
for n in soup.findAll('div'):
p = n.parent
if not p in candidates:
inner_html = n.renderContents()
if not DIV_TO_P_ELEMENTS_RE.search(inner_html):
candidates.append(n.parent)
"""
return candidates
def rank(soup):
"""Rank a soup node and its descendants."""
score = 0
score += rank_from_tagname(soup)
score += rank_from_textlengths(soup)
score += rank_from_classweight(soup)
score *= rank_by_link_density(soup)
dbg('Ranked: %s' % score)
return score
def rank_from_tagname(soup):
"""Given credit to a class depending on its tag name."""
name = soup.name
if GOOD_NAMES.match(name):
return 5
elif OK_NAMES.match(name):
return 3
elif BAD_NAMES.match(name):
return -3
elif HORRIBLE_NAMES.match(name):
return -5
return 0
def rank_from_classweight(soup):
"""Give credit if class or id matches different sets."""
score = 0
for css_type in ('class', 'id'):
if css_type in soup:
val = soup[css_type]
if NEGATIVE_RE.match(val):
score -= 50
elif POSITIVE_RE.match(val):
score += 25
return score
def rank_from_textlengths(soup):
"""Rank by length of paragraph blocks and length of text."""
score = 0
for p in soup.findAll('p'):
text = p.renderContents()
score += text.count(',')
if len(text) > 10:
score += 1
return score
def rank_by_link_density(soup):
"""Get density of links as percentage of the content.
1 - (amount of text that is inside a link)/(total text in the node).
"""
link_length = 0
for a in soup.findAll('a'):
link_length += len(_textify(a))
if not link_length:
return 1
total_length = float(len(_textify(soup)))
retval = 1 - link_length/total_length
dbg('density: %s' % retval)
return retval
def strip_junk_tags(soup):
"""Remove all elements that look fishy."""
for tag in 'table', 'ul', 'div':
for node in soup.findAll(tag):
if is_fishy(node):
node.extract()
def is_fishy(soup):
"""Return true if a node probably shouldn't be included."""
# Look at score and classweight first.
score = soup.__dict__.get('score', 0)
classweight = rank_from_classweight(soup)
if score + classweight < 0:
return True
# Next look at counts of text, images, etc.
# If there are not very many commas, and the number of
# non-paragraph elements is more than paragraphs or other ominous signs
# remove the element.
text = _textify(soup)
if text.count(',') > 10:
return False
text_length = len(text)
p_count = len(soup.findAll('p'))
img_count = len(soup.findAll('img'))
li_count = len(soup.findAll('li')) - 100
input_count = len(soup.findAll('input'))
link_density = 1.0 - rank_by_link_density(soup)
if img_count > p_count:
return True
if li_count > p_count and soup.name not in ['ul', 'ol']:
return True
if input_count > p_count / 3:
return True
if text_length < 25 and (img_count == 0 or img_count > 2):
return True
if classweight < 25 and link_density > 0.2:
return True
if classweight >= 25 and link_density > 0.5:
return True
return False
if __name__ == '__main__':
data = open(
'testdata/onion-stephen_hawking_robotic_exoskeleton.html', 'r').read()
print get_text(data)