Daniel Camargo


Script to download sequential files

04/08/2010

Sometimes I have to download sequential files from websites, specially images, and unless you are a robot you’ll agree with me that this task could be a very very boring thing to do if there are many files to download. So I built a little python script that saves sequential files in your computer.

By sequential files I mean files that have a numerical sequence on their name.

For example: trip_photo_1.jpg, trip_photo_2.jpg, trip_photo_3.jpg … trip_photo_347.jpg.

The script is:

import urllib2
import webbrowser
import os
ini = 1
end = 100
while (ini <= end):
    url = "http://www.thesite.com/folder/subfolder/file" + str(i) + ".ext"
    opener = urllib2.build_opener()
    page = opener.open(url)
    file = page.read()
    filename = "file_" + str(i) + url[-4:]
    print filename
    fout = open(filename, "wb")
    fout.write(file)
    fout.close()
    i += 1

All you need to do is configure the the values of ini, end and url variables and run this script.

ini = initial number of the sequence
end = final number of the sequence
url = url pattern

Download the script.

Bookmark and Share

Comments

  1. anon
    29/04/2010 11:43:20

    A little better.
    Keeps original filename.

    also lol @ “specially images, ”

    import urllib2
    end = 100
    for i in range(1,end+1):
    opener = urllib2.build_opener()
    page = opener.open(“http://www.thesite.com/folder/subfolder/file” + str(i) + “.ext”)
    filename = url.split(‘/’)[-1]
    fout = open(filename, “wb”)
    fout.write(page.read())
    f.close()


Leave a Reply