Python script to download pictures/Jpegs/jpgs from a website

Here is script to download images, from a website. I have taken the example of zune.net to download the images to my local disk.
you can alter it as you please:
import sgmllib
import re
class MyParser(sgmllib.SGMLParser):
A simple parser class.
def parse(self, s):
Parse the given string s.
self.feed(s)
self.close()
def __init__(self, verbose=0):
Initialise an object, passing verbose to the superclass.
sgmllib.SGMLParser.__init__(self, verbose)
self.hyperlinks = []
def start_a(self, attributes):
Process a hyperlink and its attributes.
for name, value in attributes:
if name == href:
self.hyperlinks.append(value)
def get_hyperlinks(self):
Return the list of hyperlinks.
return self.hyperlinks
import urllib, sgmllib
# Get something to work with.
f = urllib.urlopen(http://www.zune.net/en-us/mp3players/backgrounds/default.htm)
s = f.read()
# Try and process the page.
# The class should have been defined first, remember.
myparser = MyParser()
myparser.parse(s)
# Get the hyperlinks.
#print myparser.get_hyperlinks()
hlist = myparser.get_hyperlinks()
for links in hlist:
var_extn = links[-3:]
if(var_extn == jpg):
finalURL = http://www.zune.net+links
print finalURL
start = finalURL.find(480x640_)
end = finalURL.find(.jpg)
finalName = finalURL[start:end]
image = urllib.URLopener()
image.retrieve(finalURL,finalName+.jpg)

Leave a Reply

Your email address will not be published. Required fields are marked *