Get startedGet started for free

Select by class and ID with XPATH

Here's some HTML code that is similar to code you encountered in the last chapter (already read into an HTML document called weather_html):

<html>
  <body>
    <div id = 'first'>
      <h1 class = 'big'>Berlin Weather Station</h1>
      <p class = 'first'>Temperature: 20°C</p>
      <p class = 'second'>Humidity: 45%</p>
    </div>
    <div id = 'second'>...</div>
    <div id = 'third'>
      <p class = 'first'>Sunshine: 5hrs</p>
      <p class = 'second'>Precipitation: 0mm</p>
    </div>
  </body>
</html>

For this chapter, this code looks a bit more like real life. Your goal is to extract the precipitation reading from this weather station. Unfortunately, it can't be directly referenced through an ID.

Let's do this by setting up the building blocks step by step and then using them in combination!

This exercise is part of the course

Web Scraping in R

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Select all p elements
weather_html %>%
	html_elements(xpath = '___')
Edit and Run Code