From e408b8644e28d3edc0131112606f346d359cc0e2 Mon Sep 17 00:00:00 2001 From: Emanuele Cesena Date: Thu, 15 Jan 2015 11:40:25 -0800 Subject: [PATCH 1/7] Fixed empty return in top_meta_image Return empty unicode in ContentExtractor.top_meta_image() when no image is found. It was returning the article's url. Example: http://www.usnews.com/news/entertainment/articles/2015/01/15/boyhood-keaton-moore-are-oscar-favorites-in-vegas (note that USNews often updates its articles, e.g. by adding an image -- at the time of writing it was without images) --- newspaper/extractors.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/newspaper/extractors.py b/newspaper/extractors.py index 253742b3..6a9a417e 100644 --- a/newspaper/extractors.py +++ b/newspaper/extractors.py @@ -316,7 +316,9 @@ def get_meta_img_url(self, article_url, doc): top_meta_image = try_one or try_two or try_three or try_four - return urllib.parse.urljoin(article_url, top_meta_image) + if top_meta_image: + return urllib.parse.urljoin(article_url, top_meta_image) + return u'' def get_meta_type(self, doc): """Returns meta type of article, open graph protocol From 5cdc5d015d68f569bc7c0fbab3fe0cafcd699147 Mon Sep 17 00:00:00 2001 From: Emanuele Cesena Date: Thu, 15 Jan 2015 15:35:08 -0800 Subject: [PATCH 2/7] Fix nodeToString() to return valid HTML In some cases the output of nodeToString() is not valid HTML, e.g. for iframe nodes. Solution found here: http://stackoverflow.com/questions/27020950/lxml-modify-tags-prevent An example of bad behavior is an article with a youtube video inside. The video embed code is transformed into `