From d894bd347d7976db8459ad1177342376476eca38 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Wed, 16 Jun 2021 10:40:18 -0400 Subject: [PATCH] Handle error when parsing image result url --- app/filter.py | 13 ++++++++++--- app/templates/imageresults.html | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/filter.py b/app/filter.py index c56ab8d..82fc1b8 100644 --- a/app/filter.py +++ b/app/filter.py @@ -313,12 +313,19 @@ class Filter: urls = item.find('a')['href'].split('&imgrefurl=') img_url = urlparse.unquote(urls[0].replace('/imgres?imgurl=', '')) - webpage = urlparse.unquote(urls[1].split('&')[0]) + + try: + # Try to strip out only the necessary part of the web page link + web_page = urlparse.unquote(urls[1].split('&')[0]) + except IndexError: + web_page = urlparse.unquote(urls[1]) + img_tbn = urlparse.unquote(item.find('a').find('img')['src']) + results.append({ - 'domain': urlparse.urlparse(webpage).netloc, + 'domain': urlparse.urlparse(web_page).netloc, 'img_url': img_url, - 'webpage': webpage, + 'web_page': web_page, 'img_tbn': img_tbn }) diff --git a/app/templates/imageresults.html b/app/templates/imageresults.html index 17ee98a..fc09b29 100644 --- a/app/templates/imageresults.html +++ b/app/templates/imageresults.html @@ -64,7 +64,7 @@
- +
@@ -73,7 +73,7 @@
- +