From 700fdb7ef9541090a0f19c5d5682345ec86b8272 Mon Sep 17 00:00:00 2001 From: xxyzz Date: Fri, 22 Apr 2022 09:15:07 +0800 Subject: [PATCH] Fix The Economist recipe NoneType is not iterable error the "image" value could be null, for example: https://www.economist.com/special-report/2022/04/20/sources-and-acknowledgments --- recipes/economist.recipe | 9 ++++----- recipes/economist_free.recipe | 11 +++++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/recipes/economist.recipe b/recipes/economist.recipe index 25d7a6edf0..b1c17a7b0f 100644 --- a/recipes/economist.recipe +++ b/recipes/economist.recipe @@ -59,15 +59,14 @@ def load_article_from_json(raw, root): E(article, 'h1', data['headline'], style='font-size: x-large') E(article, 'div', data['description'], style='font-style: italic') E(article, 'div', (data['datePublishedString'] or '') + ' | ' + (data['dateline'] or ''), style='color: gray; margin: 1em') - images = data['image'] - if 'main' in images: + main_image_url = data.get('image', {}).get('main', {}).get('url', {}).get('canonical') + if main_image_url: div = E(article, 'div') try: - E(div, 'img', src=images['main']['url']['canonical']) + E(div, 'img', src=main_image_url) except Exception: pass - text = data['text'] - for node in text: + for node in data.get('text', []): process_node(node, article) diff --git a/recipes/economist_free.recipe b/recipes/economist_free.recipe index f36aff4d05..b1c17a7b0f 100644 --- a/recipes/economist_free.recipe +++ b/recipes/economist_free.recipe @@ -59,15 +59,14 @@ def load_article_from_json(raw, root): E(article, 'h1', data['headline'], style='font-size: x-large') E(article, 'div', data['description'], style='font-style: italic') E(article, 'div', (data['datePublishedString'] or '') + ' | ' + (data['dateline'] or ''), style='color: gray; margin: 1em') - images = data['image'] - if 'main' in images: + main_image_url = data.get('image', {}).get('main', {}).get('url', {}).get('canonical') + if main_image_url: div = E(article, 'div') try: - E(div, 'img', src=images['main']['url']['canonical']) + E(div, 'img', src=main_image_url) except Exception: pass - text = data['text'] - for node in text: + for node in data.get('text', []): process_node(node, article) @@ -236,7 +235,7 @@ class Economist(BasicNewsRecipe): def parse_index(self): # return [('Articles', [{'title':'test', - # 'url':'file:///t/raw.html' + # 'url':'https://www.economist.com/briefing/2021/09/11/how-america-wasted-its-unipolar-moment' # }])] if edition_date: url = 'https://www.economist.com/weeklyedition/' + edition_date