Getting Data from the Web using Python

Suppose you know that there is a file of data at some location on the web, and you know the URL.  Normally, you'd just type the URL into the address line of your browser and the browser will request the data from the remote web server, and then display that data in the browser window when it comes in (if it's HTML stuff), or download it into a file for you to access with some other tool (like a spreadsheet for .CSV type data).

However, now you want a Python program to retrieve this data, so it can read and digest the results...

So, here's a file, located at the following URL (you can get your browser to retrieve it if you want):  http://bert.stuy.edu/pbrooks/misc/SomeData.csv.  It contains the following data:

First name,Last Name,Height (inches)
Abe,Lincoln,78
Bill,Clinton,76
Woodrow,Wilson,73
Peter,Brooks,79

In order for Python to retrieve this file, it will need the services of the urllib2 library.  Here's how to get the data (you can try these commands in the Python shell):

import urllib2  # this will import a bunch of functions, only one of which you'll need

f=urllib2.urlopen('http://bert.stuy.edu/pbrooks/misc/SomeData.csv')  # this will retrieve the data (or cause an error if not possible)

s=f.read()  # read the data as a string into the variable 's'

# now you can manipulate s, or just print it out...
print s