Skip to content

Commit 62f58bb

Browse files
committed
Updated to handle case where a "data:image/webp" is returned instead of a regular url + transform the url to a high resolution image url
1 parent 31e5684 commit 62f58bb

1 file changed

Lines changed: 38 additions & 15 deletions

File tree

amazonscraper/client.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,19 @@ def _get_products(self, keywords="", search_url="", max_product_nb=100):
232232

233233
# Get image before url and asin
234234
css_selector = css_selector_dict.get("img", "")
235-
url_product_soup = product.select(css_selector)
236-
if url_product_soup:
237-
url = urljoin(
238-
self.base_url,
239-
url_product_soup[0].get('src'))
240-
proper_url = url.split("/ref=")[0]
241-
product_dict['img'] = proper_url
235+
img_product_soup = product.select(css_selector)
236+
if img_product_soup:
237+
img_url = img_product_soup[0].get('src')
238+
# Check if it is not a base64 formatted image
239+
if "data:image/webp" in img_url:
240+
img_url = img_product_soup[0].get(
241+
'data-search-image-source-set',
242+
'').split(' ')[0]
243+
244+
if img_url != '':
245+
img_url = _get_high_res_img_url(img_url=img_url)
246+
247+
product_dict['img'] = img_url
242248

243249
css_selector = css_selector_dict.get("url", "")
244250
url_product_soup = product.select(css_selector)
@@ -272,11 +278,28 @@ def _get_products(self, keywords="", search_url="", max_product_nb=100):
272278

273279

274280
def _css_select(soup, css_selector):
275-
""" Returns the content of the element pointed by the CSS selector,
276-
or an empty string if not found """
277-
selection = soup.select(css_selector)
278-
retour = ""
279-
if len(selection) > 0:
280-
if hasattr(selection[0], 'text'):
281-
retour = selection[0].text.strip()
282-
return retour
281+
""" Returns the content of the element pointed by the CSS selector,
282+
or an empty string if not found """
283+
selection = soup.select(css_selector)
284+
retour = ""
285+
if len(selection) > 0:
286+
if hasattr(selection[0], 'text'):
287+
retour = selection[0].text.strip()
288+
return retour
289+
290+
def _get_high_res_img_url(img_url):
291+
""" Returns a modified url pointing to the high resolution version of
292+
the image
293+
>>> print(_get_high_res_img_url("https://images-na.ssl-images-amazon.com/\
294+
images/I/513gErH1dML._AC_SX236_SY340_FMwebp_QL65_.jpg"))
295+
https://images-na.ssl-images-amazon.com/\
296+
images/I/513gErH1dML.jpg
297+
>>> print(_get_high_res_img_url("https://images-na.ssl-images-amazon.com/\
298+
images/I/51F48HFHq6L._AC_SX118_SY170_QL70_.jpg"))
299+
https://images-na.ssl-images-amazon.com/\
300+
images/I/51F48HFHq6L.jpg
301+
"""
302+
high_res_url = img_url.split("._")[0] + ".jpg"
303+
return high_res_url
304+
305+

0 commit comments

Comments
 (0)