In this example, the current temperature value is surrounded by an HTML
assigned to a specific CSS class to customize how it appears on the webpage. With the lxml
method, you can only find the
search for the specific CSS class to find the exact data, like this:
Click here to view code image
from lxml.etree.cssselect import CSSSelector
div = CSSSelector("div.'day-temp-current temp-f'")
temp = div(doctree)[0]
The CSSSelector() method specifies the HTML element and the CSS class that you’re looking
for. This is the syntax:
CSSSelector("element.class")
In this example, the class name that you’re looking for contains a space (which is allowed in CSS),
and this complicates things a bit. Because of the space, you need to place quotes around the class
name in the parameter, and you also need to place quotes around the entire value.
The CSSSelector() method sets up the item you’re searching for, and then you just need to feed
the result from the etree parser into it. The result will be a list of all the elements that match both
the HTML element and the CSS class. Hopefully, that only applies to one item in the webpage and
gives you the data you want!
Try It Yourself: Find the Current Temperature
Plenty of websites can tell you the current temperature in your city. Follow these steps
to write a Python script that contacts one of those sites, retrieves the temperature, and
then displays it:
- Find the specific website URL that has the information you’re looking for. For this
exercise, use the popular Yahoo Weather webpage to look up the current temperature
in Chicago, Illinois. If you go to the main weather.yahoo.com webpage, you have to
enter the city and state information. After you do that, you are redirected to a different
URL that contains the weather data. For Chicago, this is the URL:
Click here to view code image
http://weather.yahoo.com/unitd-states/illinois/chicago-2379574/
Make note of this, as it’s the URL that you need to use for your urlopen() method. - Use the View Source feature in your browser to look at the raw HTML code for the
webpage. Look for the data you’re interested in, and see what HTML elements are
around it. For the current temperature on the Yahoo Weather page, you might find
this:
Click here to view code image79°
Armed with the URL and the data you’re looking for, you’re ready to write the
Python script.
- Create the file script2002.py in this hour’s folder on your Raspberry Pi.