HTML 入門

R 的網頁爬蟲

Timo Grossenbacher

Instructor

看得到,就能擷取

禁止下載

R 的網頁爬蟲

超文字標記語言(HTML)

<html> 
  <body> 
    <h2>A first example</h2>
    <p>A text paragraph.</p>
    <p>
      Here follows a list:
    </p>
  </body> 
</html>

HTML 入門

R 的網頁爬蟲

HTML 具有階層結構

HTML 入門

...
    <div>
      Here follows a list:
      <ul>
        <li>Bullet 1</li>
        <li>Bullet 2</li>
        <li>Bullet 3</li>
      </ul>
    </div>
...
R 的網頁爬蟲

HTML 標籤可帶有屬性

HTML 入門

...
    <p>
      Here follows a 
      <a href="https://google.com">link</a>.
    </p>
...
R 的網頁爬蟲

用 R 讀取 HTML

library(rvest)
html <- read_html(html_document)
html
{html_document}
<html>
[1] <body> \n    <h2>A first example</h2>\n    <p>A text paragraph.</p>\n   ...
class(html)
"xml_document" "xml_node"
R 的網頁爬蟲
library(xml2)
xml_structure(html)
<html>
  <body>
    {text}
    <h2>
      {text}
    {text}
    <p>
      {text}
    {text}
    <p>
      {text}
      <a [href]>
        {text}
      {text}
    {text}
R 的網頁爬蟲

來剖析 HTML 吧!

R 的網頁爬蟲

Preparing Video For Download...