# This example demonstrates how urllib2 can be used to open websites and read # some data from them. import urllib2 # define a function which will open a bunch of links we give it in a list def open_sites(links): sites = [] for url in urls: print "Opening: " + url # try to open that site try: site = urllib2.urlopen(url) except: # Does an error occur with any of the default urls? # Practice: If so, could you fix it? print "An error has occured, skipping " + url print raw_input("...press enter key to continue...") continue if site.geturl() != url: print "Careful! Site " + url + " has redirected you to " + site.geturl() print "Site " + site.geturl() + " is now open." print sites.append(site) raw_input("...press enter key to continue...") print return sites url1 = "http://www.google.com" url2 = "http://www.sugarlabs.org" url3 = "www.wikipedia.org" urls = [url1, url2, url3] sites = open_sites(urls) print print "Let's read those sites and find their titles." print raw_input("...press enter key to continue...") print for site in sites: site_content = site.read() title_at = site_content.find("