Downloading a picture via urllib and python

后端 未结 18 1573
离开以前
离开以前 2020-11-22 10:35

So I\'m trying to make a Python script that downloads webcomics and puts them in a folder on my desktop. I\'ve found a few similar programs on here that do something simila

18条回答
  •  轮回少年
    2020-11-22 11:17

    This worked for me using python 3.

    It gets a list of URLs from the csv file and starts downloading them into a folder. In case the content or image does not exist it takes that exception and continues making its magic.

    import urllib.request
    import csv
    import os
    
    errorCount=0
    
    file_list = "/Users/$USER/Desktop/YOUR-FILE-TO-DOWNLOAD-IMAGES/image_{0}.jpg"
    
    # CSV file must separate by commas
    # urls.csv is set to your current working directory make sure your cd into or add the corresponding path
    with open ('urls.csv') as images:
        images = csv.reader(images)
        img_count = 1
        print("Please Wait.. it will take some time")
        for image in images:
            try:
                urllib.request.urlretrieve(image[0],
                file_list.format(img_count))
                img_count += 1
            except IOError:
                errorCount+=1
                # Stop in case you reach 100 errors downloading images
                if errorCount>100:
                    break
                else:
                    print ("File does not exist")
    
    print ("Done!")
    

提交回复
热议问题