0

I want donwload a image with this selector

image = response.xpath('/html/body/div/div/div[3]/div[1]/div[1]/img/@src').extract()

with this, on the shell i have the result

['data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7']

Maybe is because the website is protected with hotlinking (cloudflare protection) and i need use other method? or simply im selecting bad the image for download

Andy e
  • 1
  • 1
  • Already solved in another question:https://stackoverflow.com/questions/2323128/convert-string-in-base64-to-image-and-save-on-filesystem-in-python – Leekin Jul 31 '18 at 07:09

1 Answers1

0

After you get the base64 string:

# Assume you get this image list
image_list = ['data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7']

import base64

# Save image to file by using this code
for i, item in enumerate(image_list):
    with open("imageToSave{}.png".format(i), "wb") as fh:
    fh.write(base64.decodebytes(item.split(',')[-1]))
Leekin
  • 93
  • 1
  • 8
  • thanks but i cant assign only 1 name, because are like 400 diferents images, and with this code i got a error: AttributeError: 'list' object has no attribute 'split' – Andy e Jul 31 '18 at 07:32
  • May you can use a `for ... in ...` loop to traverse your images list, like this: ``` # Assume you get this string image_list = ['data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'] import base64 # Save image to file by using this code for i, item in enumerate(image_list): with open("imageToSave{}.png".format(i), "wb") as fh: fh.write(base64.decodebytes(item.split(',')[-1])) ``` – Leekin Aug 01 '18 at 02:14