1

The code below can download the images into my computer. However, when I open the images, the following error message appears: "It looks like we don't support this file format". There must be something wrong with my code even though the file extension is .png.

import csv
import requests

with open('rlth.csv', 'r', newline='') as file:
    has_header = csv.Sniffer().has_header(file.read(1024))
    file.seek(0)  # Rewind.
    reader = csv.reader(file)
    if has_header:
        next(reader)  # Skip header row.
    csvrows = csv.reader(file, delimiter=',', quotechar='"')
    for row in csvrows:
        filename = row[0]
        url = row[2]
        result = requests.get(url, stream=True)
        if result.status_code == 200:
            image = result.raw.read()
            open("{}.png".format(filename),"wb").write(image)

Below is a glimpse of rlth.csv:

    identifier                           link                likes_count    company
0   2293876619351464318 https://www.instagram.com/p/B_VfKG_BJl- 175     Ralph Lauren
1   2293779660171363517 https://www.instagram.com/p/B_VJHKth8S9 3263    Ralph Lauren
2   2293716498600285587 https://www.instagram.com/p/B_U6wC6hdWT 12617   Ralph Lauren
3   2293068067321995905 https://www.instagram.com/p/B_SnUIOhOKB 8430    Ralph Lauren
4   2293002309485390353 https://www.instagram.com/p/B_SYXOeBuoR 7452    Ralph Lauren
Luc
  • 21
  • 1
  • 4
  • 1
    Possibly duplicated: https://stackoverflow.com/questions/30229231/python-save-image-from-url/30229298 – Frank Apr 23 '20 at 21:07
  • Does this answer your question? [python save image from url](https://stackoverflow.com/questions/30229231/python-save-image-from-url) – Debdut Goswami Apr 23 '20 at 21:53
  • The ones that both of you mention are from url, not from a csv file with a header, right? I am not quite sure how to adapt my code.. @Frank – Luc Apr 23 '20 at 22:10
  • You code already read urls from csv and get image content by requests.get(url). What doesn't work in your code is just how to save the image. – Frank Apr 24 '20 at 02:02

0 Answers0