Kelas dan ID CSS

Web Scraping di R

Timo Grossenbacher

Instructor

Kelas

.alert {
  color: red;
  font-weight: 800;
}
...
<div>Beberapa teks.</div>
<div class = 'alert'>Teks penting.</div>  
<div>
  Beberapa teks dengan
  <a href = '#' class = 'alert'>tautan penting</a>.
</div>
...

HTML dengan kelas

html %>% html_elements('.alert')
{xml_nodeset (2)}
[1] <div class="alert">Important text...
[2] <a href="#" class="alert">important ...
Web Scraping di R

Memilih beberapa kelas sekaligus

.alert {
  color: red;
  font-weight: 800;
}
.emph {
  font-style: italic;
}
...
<div>Beberapa teks.</div>
<div class = 'alert emph'>Teks penting.</div>  
<div>
  Beberapa teks dengan
  <a href = '#' class = 'alert'>tautan penting</a>.
</div>
...

Beberapa kelas

html %>% 
    html_elements('.alert.emph') # bukan: .alert, .emph
{xml_nodeset (1)}
[1] <div class="alert emph">Important text...
Web Scraping di R

ID

#special {
  color: green;
}
.alert {
  color: red;
  font-weight: 800;
}
...
<div id = 'special'>Beberapa teks.</div>
<div class = 'alert'>Teks penting.</div>  
<div>
  Beberapa teks dengan
  <a href = '#' class = 'alert'>tautan penting</a>.
</div>
...

HTML dengan ID

html %>% 
  html_elements('#special')
{xml_nodeset (1)}
[1] <div id="special">Some text.</div>
Web Scraping di R

Mempersempit seleksi dengan tipe

#special {
  color: green;
}
.alert {
  color: red;
  font-weight: 800;
}
...
<div id = 'special'>Beberapa teks.</div>
<div class = 'alert'>Teks penting.</div>  
<div>
  Beberapa teks dengan
  <a href = '#' class = 'alert'>tautan penting</a>.
</div>
...
html %>% 
  html_elements('a.alert')
{xml_nodeset (1)}
[1] <a href="#" class="alert">important ...
html %>% 
  html_elements('#special')

setara dengan...

html %>% 
  html_elements('div#special')
Web Scraping di R

Pseudo-class untuk memilih anak tertentu

li:first-child { color: blue; }

li:nth-child(2) { color: green; }

li:last-child { color: red; }
...
<ol>
  <li>Elemen pertama.</li>
  <li>Elemen kedua.</li>
  <li>Elemen ketiga.</li>
</ol>
...

Kelas semu CSS

html %>% html_elements('li:last-child') 
    # atau html_elements('li:nth-child(3)')
{xml_nodeset (1)}
[1] <li>Elemen ketiga.</li>
1 https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes
Web Scraping di R

Ringkasannya...

Jenis selektor HTML Selektor CSS
Tipe <p>...</p> p
Multi-tipe <p>...</p><div>...</div> p, div
Kelas <p class = 'x'>...</p> .x
Multi-kelas <p class = 'x y'>...</p> .x.y
Tipe + Kelas <p class = 'x'>...</p> p.x
ID <p id = 'x'>...</p> #x
Tipe + Pseudo-class <p>...</p><p>...</p> p:first-child
Web Scraping di R

Ayo berlatih!

Web Scraping di R

Preparing Video For Download...