Web Scraping em R
Timo Grossenbacher
Instructor
.alert {
color: red;
font-weight: 800;
}
...
<div>Algum texto.</div>
<div class = 'alert'>Texto importante.</div>
<div>
Algum texto com um
<a href = '#' class = 'alert'>link importante</a>.
</div>
...

html %>% html_elements('.alert')
{xml_nodeset (2)}
[1] <div class="alert">Important text...
[2] <a href="#" class="alert">important ...
.alert {
color: red;
font-weight: 800;
}
.emph {
font-style: italic;
}
...
<div>Algum texto.</div>
<div class = 'alert emph'>Texto importante.</div>
<div>
Algum texto com um
<a href = '#' class = 'alert'>link importante</a>.
</div>
...

html %>%
html_elements('.alert.emph') # não: .alert, .emph
{xml_nodeset (1)}
[1] <div class="alert emph">Important text...
#special {
color: green;
}
.alert {
color: red;
font-weight: 800;
}
...
<div id = 'special'>Algum texto.</div>
<div class = 'alert'>Texto importante.</div>
<div>
Algum texto com um
<a href = '#' class = 'alert'>link importante</a>.
</div>
...

html %>%
html_elements('#special')
{xml_nodeset (1)}
[1] <div id="special">Some text.</div>
#special {
color: green;
}
.alert {
color: red;
font-weight: 800;
}
...
<div id = 'special'>Algum texto.</div>
<div class = 'alert'>Texto importante.</div>
<div>
Algum texto com um
<a href = '#' class = 'alert'>link importante</a>.
</div>
...
html %>%
html_elements('a.alert')
{xml_nodeset (1)}
[1] <a href="#" class="alert">important ...
html %>%
html_elements('#special')
é equivalente a...
html %>%
html_elements('div#special')
li:first-child { color: blue; }
li:nth-child(2) { color: green; }
li:last-child { color: red; }
...
<ol>
<li>Primeiro elemento.</li>
<li>Segundo elemento.</li>
<li>Terceiro elemento.</li>
</ol>
...

html %>% html_elements('li:last-child')
# ou html_elements('li:nth-child(3)')
{xml_nodeset (1)}
[1] <li>Third element.</li>
| Tipo de seletor | HTML | Seletor CSS |
|---|---|---|
| Tipo | <p>...</p> |
p |
| Vários tipos | <p>...</p><div>...</div> |
p, div |
| Classe | <p class = 'x'>...</p> |
.x |
| Várias classes | <p class = 'x y'>...</p> |
.x.y |
| Tipo + Classe | <p class = 'x'>...</p> |
p.x |
| ID | <p id = 'x'>...</p> |
#x |
| Tipo + Pseudo-classe | <p>...</p><p>...</p> |
p:first-child |
Web Scraping em R