CSS sınıfları ve ID’ler

R ile Web Kazıma

Timo Grossenbacher

Instructor

Sınıflar

.alert {
  color: red;
  font-weight: 800;
}
...
<div>Some text.</div>
<div class = 'alert'>Important text.</div>  
<div>
  Some text with an
  <a href = '#' class = 'alert'>important link</a>.
</div>
...

Sınıflı HTML

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

Birden çok sınıfı birlikte seçme

.alert {
  color: red;
  font-weight: 800;
}
.emph {
  font-style: italic;
}
...
<div>Some text.</div>
<div class = 'alert emph'>Important text.</div>  
<div>
  Some text with an
  <a href = '#' class = 'alert'>important link</a>.
</div>
...

Birden çok sınıf

html %>% 
    html_elements('.alert.emph') # değil: .alert, .emph
{xml_nodeset (1)}
[1] <div class="alert emph">Important text...
R ile Web Kazıma

ID’ler

#special {
  color: green;
}
.alert {
  color: red;
  font-weight: 800;
}
...
<div id = 'special'>Some text.</div>
<div class = 'alert'>Important text.</div>  
<div>
  Some text with an
  <a href = '#' class = 'alert'>important link</a>.
</div>
...

ID içeren HTML

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

Türlerle seçimi daraltma

#special {
  color: green;
}
.alert {
  color: red;
  font-weight: 800;
}
...
<div id = 'special'>Some text.</div>
<div class = 'alert'>Important text.</div>  
<div>
  Some text with an
  <a href = '#' class = 'alert'>important link</a>.
</div>
...
html %>% 
  html_elements('a.alert')
{xml_nodeset (1)}
[1] <a href="#" class="alert">important ...
html %>% 
  html_elements('#special')

eşdeğerdir...

html %>% 
  html_elements('div#special')
R ile Web Kazıma

Belirli çocukları seçmek için sözde sınıflar

li:first-child { color: blue; }

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

li:last-child { color: red; }
...
<ol>
  <li>First element.</li>
  <li>Second element.</li>
  <li>Third element.</li>
</ol>
...

CSS sözde sınıfları

html %>% html_elements('li:last-child') 
    # ya da html_elements('li:nth-child(3)')
{xml_nodeset (1)}
[1] <li>Third element.</li>
1 https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes
R ile Web Kazıma

Özetle...

Seçici türü HTML CSS seçici
Tür <p>...</p> p
Birden çok tür <p>...</p><div>...</div> p, div
Sınıf <p class = 'x'>...</p> .x
Çoklu sınıf <p class = 'x y'>...</p> .x.y
Tür + Sınıf <p class = 'x'>...</p> p.x
ID <p id = 'x'>...</p> #x
Tür + Sözde sınıf <p>...</p><p>...</p> p:first-child
R ile Web Kazıma

Hadi pratik yapalım!

R ile Web Kazıma

Preparing Video For Download...