Page 108 -
P. 108

textual data






                           With the appropriate function identified, you were to amend the code to control how often the
                           request for the web page is sent to the server. The Beans’R’Us webmaster has been in touch
                           to say that their web-based pricing information is updated every 15 minutes.You were to fill in
                           the blanks in the code as indicated by the dashed lines.
                           Hints: 15 minutes equates to 15 multiplied by 60 seconds, which is 900 seconds. Also: to use
                           the functionality provided by a library, remember to import it first.

         Import the library at the top
         of the program. This gives the
          program access to all the built-in
          functionality that the library
          provides.


          import urllib.request
           import time



                                        Use the facilities of the time
           price = 99.99
                                        library to pause the program for
           while price > 4.74:          15 minutes between requests.
                time.sleep(900)

               page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")
               text = page.read().decode("utf8")

               where = text.find('>$')
               start_of_price = where + 2
               end_of_price = start_of_price + 4
               price = float(text[start_of_price:end_of_price])

           print ("Buy!")

















                                                                                        you are here 4    73
   103   104   105   106   107   108   109   110   111   112   113