Page 67 - Programming the Raspberry Pi Getting Started with Python
P. 67
Note that you will need to execute the read line as soon as possible after opening the URL. What
you have done here is to send a web request to www.amazon.com, asking it to search on “raspberry
pi.” This has sent back the HTML for Amazon’s web page that would display (if you were using a
browser) the list of search results.
If you look carefully at the structure of this web page, you can see that you can use it to provide a
list of Raspberry Pi–related items found by Amazon. If you scroll around the text, you will find some
lines like these:
They key thing here is <div class="productTitle">. There is one instance of this before each of
the search results. (It helps to have the same web page open in a browser for comparison.) What you
want to do is copy out the actual title text. You could do this by finding the position of the text
productTitle, counting two > characters, and then taking the text from that position until the next <
character, like so:
When you run this, you will mostly get a list of products. If you really get into this kind of thing,
then search for “Regular Expressions in Python” on the Internet. Regular expressions are almost a