I want to downloads images frp, multiple URLs from a column in a csv file and store it in a folder in my computer
I tried this does not work and give me errors:
import urllib.request
import csv
import os
with open('booklogo.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
name, ext = os.path.splitext(row['link'])
title_filename = f"{row['title']}{ext}".replace('/', '-')
urllib.request.urlretrieve(row['link'], title_filename)
CodePudding user response:
name, ext = os.path.splitext(row['link'])
this splits the URL and assigns the extension to ext
. but the URLs ( atleast those in image ) does not have an extension so ext
is empty.
try this
import urllib.request
import csv
import os
with open('booklogo.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)
if row["link"] != '' and row["title"] != '':
name, ext = os.path.splitext(row['link'])
if ext == '':
ext = ".png"
title_filename = f"{row['title']}{ext}".replace('/', '-')
urllib.request.urlretrieve(row['link'], title_filename)