Page 106 -
P. 106

textual data






                            With the appropriate function identified, 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. 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 urllib.request





           price = 99.99

           while price > 4.74:

              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    71
   101   102   103   104   105   106   107   108   109   110   111