Page 81 -
P. 81

extract the price





                                               You needed to update the program to extract the price starting at
                                               the 235th character of the string. The price is four characters long
                                               and stored in a variable called price.







                                          import urllib.request

                                          page = urllib.request.urlopen(“http://www.beans-r-us.biz/prices.html")
                                          text = page.read().decode(“utf8")
                 You store the substring in   price = text[234:238]
                  the “price" variable.   print(price)

                  Now, instead of printing
                  “text" (the entire web                 The substring includes characters at indexes 234, 235,
                   page), you are just                   236, and 237. Note how you’ve specified the substring
                   printing “price" (the                 with two index values, separated by the “:" character.
                   extracted substring).







                                                                                238 is the second index value
                                                                                used in the substring specification
                                                                               because it is after the last
                 This is the HTML text         This is the first character     character to be extracted.
                                               in the required substring,
                 leading up to the price.
                                               at index 234.












                                                                        Here it is: the substring to
                                                                         extract and the current
                                                                         price of coffee.




           46    Chapter 2
   76   77   78   79   80   81   82   83   84   85   86