From f678388dbc7d90082e7df266402711006c3818be Mon Sep 17 00:00:00 2001 From: Vlad <66268477+gangural@users.noreply.github.com> Date: Sat, 8 Aug 2020 20:35:22 +0300 Subject: [PATCH] Fix google images 'get image' button bug from issue #2103 (#2115) Closes #2103 --- searx/engines/google_images.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/searx/engines/google_images.py b/searx/engines/google_images.py index 6ec242b53..f0e9e27e3 100644 --- a/searx/engines/google_images.py +++ b/searx/engines/google_images.py @@ -24,6 +24,7 @@ Definitions`_. """ +import urllib from lxml import html from flask_babel import gettext from searx import logger @@ -77,6 +78,19 @@ def scrap_out_thumbs(dom): return ret_val +def scrap_img_by_id(script, data_id): + """Get full image URL by data-id in parent element + """ + img_url = '' + _script = script.split('\n') + for i, line in enumerate(_script): + if 'gstatic.com/images' in line and data_id in line: + url_line = _script[i + 1] + img_url = url_line.split('"')[1] + img_url = urllib.parse.unquote(img_url.replace(r'\u00', r'%')) + return img_url + + def request(query, params): """Google-Video search request""" @@ -133,6 +147,7 @@ def response(resp): # convert the text to dom dom = html.fromstring(resp.text) img_bas64_map = scrap_out_thumbs(dom) + img_src_script = eval_xpath(dom, '//script[contains(., "AF_initDataCallback({key: ")]')[1].text # parse results # @@ -142,8 +157,7 @@ def response(resp): #