Skip to content

Commit cd391bd

Browse files
committed
Try to select main image from the header of an article or close (not only article text).
1 parent 7877e07 commit cd391bd

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

readability/readability.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ def _get_main_image_dict(self, html_string):
142142

143143
# Transform the html string into an lxml tree
144144
doc = build_doc(html_string)
145+
146+
if len(list(self.tags(doc, 'img'))) == 0:
147+
doc = build_doc(tounicode(self.html))
148+
new_html = clean_attributes(tounicode(self._parse(self.content())))
149+
haystack = html_string.strip()
150+
while haystack not in new_html and len(haystack) > 20:
151+
old_len = len(haystack)
152+
haystack = haystack[old_len/4 : old_len/2+old_len/4]
153+
#log.info(haystack)
154+
doc = build_doc(new_html.split(haystack)[0])
145155

146156
# Build list of img tags
147157
tags = []

0 commit comments

Comments
 (0)