Tuesday, November 16, 2010

Download files - Python

use of this code is primarily to download imaes from a certain url(say you have the urls saved as a text file...sample would be something like this...

**i got this from the site gnome arts** where the second part of a line is a image

////////////////////
[IMG] ABSTRACT-AbstractChaos_1280x1024.jpg                          19-May-2006 06:05  893K 
[IMG] ABSTRACT-AbstractChaosgold_1024x768.jpg                       19-May-2006 06:05  411K 
[IMG] ABSTRACT-AbstractChaosgold_1280x1024.jpg                      19-May-2006 06:06  794K 
[IMG] ABSTRACT-AbstractGNOME_1024x768.png                           22-Dec-2006 06:05  1.4M 
[IMG] ABSTRACT-Abstract_1024x768.png                                15-Jun-2007 14:06  843K 
[IMG] ABSTRACT-AnotherBlueRidge_1024x768.png                        26-Jul-2007 14:05  929K 
///////////////////

the python script that is used to download the images and save it to a folder "scripts/downloads" is given below

import urllib,os
import os

#the name of the file where you have the url in the above defined format
f = open('./scripts/url.txt','r')
lines = f.read()
#changing the directory so that you downloaded files can be saved here
os.chdir('./scripts/download/')

#loop thru
for line in lines.split('\n'):
    if not line == "":
    #the url link from where you have to download
        url = 'http://ftp.gnome.org/pub/GNOME/teams/art.gnome.org/backgrounds/'+line.split(' ')[1]
        message = urllib.urlretrieve(url,line.split(' ')[1])
        print "Downloaded :",message

print "downloaded..."


# iam sorry for the poor formatting(which is non-python)...the editor actually removes all the indentation part of the text

No comments:

Post a Comment