MulaiMulai sekarang secara gratis

Get to know the position() function

As you saw in the video, the position() function is very powerful when used within a predicate. Together with operators, you can basically select any node from those that match a certain path.

You'll try this out with the following HTML excerpt that is available to you via rules_html. Let's assume this is a continuously updated website that displays certain Coronavirus rules for a given day and the day after.

...
<div>
  <h2>Today's rules</h2>
  <p>Wear a mask</p>
  <p>Wash your hands</p>
</div>
<div>
  <h2>Tomorrow's rules</h2>
  <p>Wear a mask</p>
  <p>Wash your hands</p>
  <small>Bring hand sanitizer with you</small>
</div>
...

Latihan ini adalah bagian dari kursus

Web Scraping in R

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Select the text of the second p in every div
rules_html %>% 
  html_elements(xpath = ___) %>%
  ___
Edit dan Jalankan Kode