CommencerCommencer gratuitement

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!

Cet exercice fait partie du cours

Web Scraping in R

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Select all p elements
weather_html %>%
	html_elements(xpath = '___')
Modifier et exécuter le code