ComenzarEmpieza gratis

Extract nodes based on the number of their children

As shown in the video, the XPATH count() function can be used within a predicate to narrow down a selection to these nodes that match a certain children count. This is especially helpful if your scraper depends on some nodes having a minimum amount of children.

Here's an excerpt from a page (without any classes or IDs…) that you might be scraping:

...
<div>
  <h1>Tomorrow</h1>
</div>
<div>
  <h2>Berlin</h2>
  <p>Temperature: 20°C</p>
  <p>Humidity: 50%</p>
</div>
<div>
  <h2>London</h2>
  <p>Temperature: 15°C</p>
</div>
<div>
  <h2>Zurich</h2>
  <p>Temperature: 22°C</p>
  <p>Humidity: 60%</p>
</div>
...

You're only interested in divs that have exactly one h2 header and at least two paragraphs, because your application can't really deal with incomplete weather forecasts.

The above HTML is available to you via forecast_html.

Este ejercicio forma parte del curso

Web Scraping in R

Ver curso

Instrucciones del ejercicio

  • Select the desired divs with the appropriate XPATH selector, making use of the count() function.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Select only divs with one header and at least two paragraphs
forecast_html %>%
	html_elements(xpath = '___')
Editar y ejecutar código