Retrieving Images from HTML Documents : Parse HTML « Network « Python Tutorial






import HTMLParser
import urllib
import sys

urlString = "http://www.python.org"

def getImage(addr):
    u = urllib.urlopen(addr)
    data = u.read()

    splitPath = addr.split('/')
    fName = splitPath.pop()
    print fName

    f = open(fName, 'wb')
    f.write(data)
    f.close()

class parseImages(HTMLParser.HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == 'img':
            for name,value in attrs:
                if name == 'src':
                    getImage(urlString + "/" + value)

lParser = parseImages()

u = urllib.urlopen(urlString)
print u.info()

lParser.feed(u.read())
lParser.close()








21.21.Parse HTML
21.21.1.Extract list of URLs in a web page
21.21.2.Opening HTML Documents
21.21.3.Retrieving Links from HTML Documents
21.21.4.Retrieving Images from HTML Documents
21.21.5.Retrieving Text from HTML Documents
21.21.6.Retrieving Cookies in HTML Documents
21.21.7.Adding Quotes to Attribute Values in HTML Documents
21.21.8.Basic HTML Title Retriever
21.21.9.HTML Title Retriever With Entity Support