forked from kingwkb/readability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readability.py
336 lines (259 loc) · 10.9 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
#coding=utf-8
# author: kingwkb
# blog : http://yanghao.org/blog/
#
# this is code demo: http://yanghao.org/tools/readability
from __future__ import division
import os
import sys
import urllib
import urlparse
import re
import HTMLParser
import math
import urlparse
import posixpath
import chardet
from BeautifulSoup import BeautifulSoup
#from bs4 import BeautifulSoup
class Readability:
regexps = {
'unlikelyCandidates': re.compile("combx|comment|community|disqus|extra|foot|header|menu|"
"remark|rss|shoutbox|sidebar|sponsor|ad-break|agegate|"
"pagination|pager|popup|tweet|twitter",re.I),
'okMaybeItsACandidate': re.compile("and|article|body|column|main|shadow", re.I),
'positive': re.compile("article|body|content|entry|hentry|main|page|pagination|post|text|"
"blog|story",re.I),
'negative': re.compile("combx|comment|com|contact|foot|footer|footnote|masthead|media|"
"meta|outbrain|promo|related|scroll|shoutbox|sidebar|sponsor|"
"shopping|tags|tool|widget", re.I),
'extraneous': re.compile("print|archive|comment|discuss|e[\-]?mail|share|reply|all|login|"
"sign|single",re.I),
'divToPElements': re.compile("<(a|blockquote|dl|div|img|ol|p|pre|table|ul)",re.I),
'replaceBrs': re.compile("(<br[^>]*>[ \n\r\t]*){2,}",re.I),
'replaceFonts': re.compile("<(/?)font[^>]*>",re.I),
'trim': re.compile("^\s+|\s+$",re.I),
'normalize': re.compile("\s{2,}",re.I),
'killBreaks': re.compile("(<br\s*/?>(\s| ?)*)+",re.I),
'videos': re.compile("http://(www\.)?(youtube|vimeo)\.com",re.I),
'skipFootnoteLink': re.compile("^\s*(\[?[a-z0-9]{1,2}\]?|^|edit|citation needed)\s*$",re.I),
'nextLink': re.compile("(next|weiter|continue|>([^\|]|$)|»([^\|]|$))",re.I),
'prevLink': re.compile("(prev|earl|old|new|<|«)",re.I)
}
def __init__(self, input, url):
"""
url = "http://yanghao.org/blog/"
htmlcode = urllib2.urlopen(url).read().decode('utf-8')
readability = Readability(htmlcode, url)
print readability.title
print readability.content
"""
self.candidates = {}
self.input = input
self.url = url
self.input = self.regexps['replaceBrs'].sub("</p><p>",self.input)
self.input = self.regexps['replaceFonts'].sub("<\g<1>span>",self.input)
self.html = BeautifulSoup(self.input)
# print self.html.originalEncoding
# print self.html
self.removeScript()
self.removeStyle()
self.removeLink()
self.title = self.getArticleTitle()
self.content = self.grabArticle()
def removeScript(self):
for elem in self.html.findAll("script"):
elem.extract()
def removeStyle(self):
for elem in self.html.findAll("style"):
elem.extract()
def removeLink(self):
for elem in self.html.findAll("link"):
elem.extract()
def grabArticle(self):
for elem in self.html.findAll(True):
unlikelyMatchString = elem.get('id','')+elem.get('class','')
if self.regexps['unlikelyCandidates'].search(unlikelyMatchString) and \
not self.regexps['okMaybeItsACandidate'].search(unlikelyMatchString) and \
elem.name != 'body':
# print elem
# print '--------------------'
elem.extract()
continue
# pass
if elem.name == 'div':
s = elem.renderContents(encoding=None)
if not self.regexps['divToPElements'].search(s):
elem.name = 'p'
for node in self.html.findAll('p'):
parentNode = node.parent
grandParentNode = parentNode.parent
innerText = node.text
# print '=================='
# print node
# print '------------------'
# print parentNode
if not parentNode or len(innerText) < 20:
continue
parentHash = hash(str(parentNode))
grandParentHash = hash(str(grandParentNode))
if parentHash not in self.candidates:
self.candidates[parentHash] = self.initializeNode(parentNode)
if grandParentNode and grandParentHash not in self.candidates:
self.candidates[grandParentHash] = self.initializeNode(grandParentNode)
contentScore = 1
contentScore += innerText.count(',')
contentScore += innerText.count(u',')
contentScore += min(math.floor(len(innerText) / 100), 3)
self.candidates[parentHash]['score'] += contentScore
# print '======================='
# print self.candidates[parentHash]['score']
# print self.candidates[parentHash]['node']
# print '-----------------------'
# print node
if grandParentNode:
self.candidates[grandParentHash]['score'] += contentScore / 2
topCandidate = None
for key in self.candidates:
# print '======================='
# print self.candidates[key]['score']
# print self.candidates[key]['node']
self.candidates[key]['score'] = self.candidates[key]['score'] * \
(1 - self.getLinkDensity(self.candidates[key]['node']))
if not topCandidate or self.candidates[key]['score'] > topCandidate['score']:
topCandidate = self.candidates[key]
content = ''
if topCandidate:
content = topCandidate['node']
# print content
content = self.cleanArticle(content)
return content
def cleanArticle(self, content):
self.cleanStyle(content)
self.clean(content, 'h1')
self.clean(content, 'object')
self.cleanConditionally(content, "form")
if len(content.findAll('h2')) == 1:
self.clean(content, 'h2')
self.clean(content, 'iframe')
self.cleanConditionally(content, "table")
self.cleanConditionally(content, "ul")
self.cleanConditionally(content, "div")
self.fixImagesPath(content)
content = content.renderContents(encoding=None)
content = self.regexps['killBreaks'].sub("<br />", content)
return content
def clean(self,e ,tag):
targetList = e.findAll(tag)
isEmbed = 0
if tag =='object' or tag == 'embed':
isEmbed = 1
for target in targetList:
attributeValues = ""
for attribute in target.attrs:
attributeValues += target[attribute[0]]
if isEmbed and self.regexps['videos'].search(attributeValues):
continue
if isEmbed and self.regexps['videos'].search(target.renderContents(encoding=None)):
continue
target.extract()
def cleanStyle(self, e):
for elem in e.findAll(True):
del elem['class']
del elem['id']
del elem['style']
def cleanConditionally(self, e, tag):
tagsList = e.findAll(tag)
for node in tagsList:
weight = self.getClassWeight(node)
hashNode = hash(str(node))
if hashNode in self.candidates:
contentScore = self.candidates[hashNode]['score']
else:
contentScore = 0
if weight + contentScore < 0:
node.extract()
else:
p = len(node.findAll("p"))
img = len(node.findAll("img"))
li = len(node.findAll("li"))-100
input = len(node.findAll("input"))
embedCount = 0
embeds = node.findAll("embed")
for embed in embeds:
if not self.regexps['videos'].search(embed['src']):
embedCount += 1
linkDensity = self.getLinkDensity(node)
contentLength = len(node.text)
toRemove = False
if img > p:
toRemove = True
elif li > p and tag != "ul" and tag != "ol":
toRemove = True
elif input > math.floor(p/3):
toRemove = True
elif contentLength < 25 and (img==0 or img>2):
toRemove = True
elif weight < 25 and linkDensity > 0.2:
toRemove = True
elif weight >= 25 and linkDensity > 0.5:
toRemove = True
elif (embedCount == 1 and contentLength < 35) or embedCount > 1:
toRemove = True
if toRemove:
node.extract()
def getArticleTitle(self):
title = ''
try:
title = self.html.find('title').text
except:
pass
return title
def initializeNode(self, node):
contentScore = 0
if node.name == 'div':
contentScore += 5;
elif node.name == 'blockquote':
contentScore += 3;
elif node.name == 'form':
contentScore -= 3;
elif node.name == 'th':
contentScore -= 5;
contentScore += self.getClassWeight(node)
return {'score':contentScore, 'node': node}
def getClassWeight(self, node):
weight = 0
if 'class' in node:
if self.regexps['negative'].search(node['class']):
weight -= 25
if self.regexps['positive'].search(node['class']):
weight += 25
if 'id' in node:
if self.regexps['negative'].search(node['id']):
weight -= 25
if self.regexps['positive'].search(node['id']):
weight += 25
return weight
def getLinkDensity(self, node):
links = node.findAll('a')
textLength = len(node.text)
if textLength == 0:
return 0
linkLength = 0
for link in links:
linkLength += len(link.text)
return linkLength / textLength
def fixImagesPath(self, node):
imgs = node.findAll('img')
for img in imgs:
src = img.get('src',None)
if not src:
img.extract()
continue
if 'http://' != src[:7] and 'https://' != src[:8]:
newSrc = urlparse.urljoin(self.url, src)
newSrcArr = urlparse.urlparse(newSrc)
newPath = posixpath.normpath(newSrcArr[2])
newSrc = urlparse.urlunparse((newSrcArr.scheme, newSrcArr.netloc, newPath,
newSrcArr.params, newSrcArr.query, newSrcArr.fragment))
img['src'] = newSrc