Aan de slagGa gratis aan de slag

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!

Deze oefening maakt deel uit van de cursus

Web Scraping in R

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Select all p elements
weather_html %>%
	html_elements(xpath = '___')
Code bewerken en uitvoeren