Introduzione a HTML

Web scraping in R

Timo Grossenbacher

Instructor

Se lo vedi, puoi fare scraping

niente download

Web scraping in R

Hypertext Markup Language (HTML)

<html> 
  <body> 
    <h2>Un primo esempio</h2>
    <p>Un paragrafo di testo.</p>
    <p>
      Ecco un elenco:
    </p>
  </body> 
</html>

Intro HTML

Web scraping in R

HTML è organizzato in modo gerarchico

Intro HTML

...
    <div>
      Ecco un elenco:
      <ul>
        <li>Punto 1</li>
        <li>Punto 2</li>
        <li>Punto 3</li>
      </ul>
    </div>
...
Web scraping in R

I tag HTML possono avere attributi

Intro HTML

...
    <p>
      Qui c’è un 
      <a href="https://google.com">link</a>.
    </p>
...
Web scraping in R

Leggere HTML con R

library(rvest)
html <- read_html(html_document)
html
{html_document}
<html>
[1] <body> \n    <h2>Un primo esempio</h2>\n    <p>Un paragrafo di testo.</p>\n   ...
class(html)
"xml_document" "xml_node"
Web scraping in R
library(xml2)
xml_structure(html)
<html>
  <body>
    {text}
    <h2>
      {text}
    {text}
    <p>
      {text}
    {text}
    <p>
      {text}
      <a [href]>
        {text}
      {text}
    {text}
Web scraping in R

Facciamo il parsing di HTML!

Web scraping in R

Preparing Video For Download...