Skip to content

11.7 11.8 With Statements and Web Fetching

  • The with method automatically closes the file when finished, which is good, cause people forget
  • Context Manager
  • Automates the process of doing common operations at the start of some tasks, as well as automating some operations at the end of some tasks.
  • The Python with statement makes using context managers easy. The general form of a with statement is:
    with <create some object that understands context> as <some name>:
        do some stuff with the object
        ...
    
  • When the program exits the with block, the context manager handles the common stuff that normally happens. For example closing a file.

Example

mydata.txt
1 2 3
4 5 6 
with open('mydata.txt') as md:
    print(md)
    for line in md:
        print(line)
print(md)
<openfile 'mydata.txt', mode 'r'>
1 2 3

4 5 6

<closedfile 'mydata.txt', mode 'r'>

11.8 Fetching Something From The Web

  • We’ll need permission to write to the destination filename, and the file will be created in the “current directory” - i.e. the same folder that the Python program is saved in.
  • If we are behind a proxy server that requires authentication, (as some students are), this may require some more special handling to work around our proxy. Use a local resource for the purpose of this demonstration!
  • We will try to retrieve the content of the HTML of this page as in the following code.
    import urllib.request
    
    def retrieve_page(url):
        """ Retrieve the contents of a web page.
        """
        my_socket = urllib.request.urlopen(url)
        dta = my_socket.read()
        return dta
    
    the_text = retrieve_page("https://runestone.academy/runestone/books/published/thinkcspy/Files/FetchingSomethingFromTheWeb.html")
    print(the_text)