''' Purpose: introduce web data acquisition -- print contents of the file word-of-the-day from web folder http://www.cs.virginia.edu/~cs1112/datasets/words/ ''' # need help to get web data - so import the capability from urllib.request import urlopen # IMPORTANT CONSTANTS # this is our base url for the folder words on our class website # it has several lists, dictionaries that we can access CS1112_WORDS_WEB_FOLDER = 'http://www.cs.virginia.edu/~cs1112/datasets/words/' # this is the file we want to access within our words folder FILE_NAME = 'word-of-the-day' # get a link to file of interest # we can glue the base url and the file_name --> we have a link to a particular file on the web # 'http://www.cs.virginia.edu/~cs1112/datasets/words/word-of-the-day' link = CS1112_WORDS_WEB_FOLDER + FILE_NAME # get a connection to stream the web resource of interest stream = urlopen( link ) # use this function first! allows us to open the finished link to get the stream # read stream to gets its encoded contents page = stream.read() # read in the stream from the website # decode page into plain text form text = page.decode() # get the decoded version of the page --> give you back a string! # clean up text to get the word - remove whitespace word = text.strip() # print word of the day print( 'word of the day:', word ) print( 'text =', text )