14.1 Connect to URL

urllib2 is a python module that provides utilities for connecting to web addresses and retrieving data from them.

import urllib2
 
def main():
    # open a connection to a URL using urllib2
    webUrl = urllib2.urlopen("http://bloomberg.com")
 
    # get the result code and print it
    print "result code: " + str(webUrl.getcode())
 
    # read the data from the URL and print it
    data = webUrl.read()
    print data
 
if __name__ == "__main__":
    main()