From cba19f209bad8a58bbc502b8c8ea7f5cdc9f6b6c Mon Sep 17 00:00:00 2001 From: Jerry Charumilind Date: Tue, 5 Jul 2011 17:17:38 -0700 Subject: [PATCH] Fix issue with trying to drop root node remove_unlikely_candidates would try to drop_tree the root node if it deemed it an unlikely candidate. This prevents that from happening. --- readability/readability.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/readability/readability.py b/readability/readability.py index a6534ed..75a1be0 100644 --- a/readability/readability.py +++ b/readability/readability.py @@ -295,7 +295,11 @@ class Document: for elem in self.html.iter(): s = "%s %s" % (elem.get('class', ''), elem.get('id', '')) #self.debug(s) - if REGEXES['unlikelyCandidatesRe'].search(s) and (not REGEXES['okMaybeItsACandidateRe'].search(s)) and elem.tag != 'body': + if (REGEXES['unlikelyCandidatesRe'].search(s) and + (not REGEXES['okMaybeItsACandidateRe'].search(s)) and + elem.tag != 'body' and + elem.getparent() is not None + ): self.debug("Removing unlikely candidate - %s" % describe(elem)) elem.drop_tree()