Skip to content

Instantly share code, notes, and snippets.

@kndrio
Forked from davidbauer/gist:11055010
Last active December 4, 2019 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kndrio/49d589b6d09cdebcc5d7bf747ab11b6b to your computer and use it in GitHub Desktop.
Save kndrio/49d589b6d09cdebcc5d7bf747ab11b6b to your computer and use it in GitHub Desktop.
Python script to download images from a CSV of image urls
#!/usr/bin/env python
# assuming a csv file with a name in column 0 and the image url in column 1
import urllib
import ntpath
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
filename = "deputados-img"
# open file to read
with open("{0}.csv".format(filename), 'r') as csvfile:
# iterate on all lines
i = 0
for line in csvfile:
splitted_line = line.split(',')
img_filename = path_leaf(splitted_line[1])
# check if we have an image URL
if splitted_line[1] != '' and splitted_line[1] != "\n":
urllib.urlretrieve(splitted_line[1], '{0}'.format(img_filename.rstrip("\r\n")))
print "Image saved for {0}".format(splitted_line[0])
print "Filename: " + img_filename
i += 1
else:
print "No result for {0}".format(splitted_line[0])
@kndrio
Copy link
Author

kndrio commented Dec 4, 2019

Now the script preserve the original filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment