CSS 简介

R 语言网页抓取

Timo Grossenbacher

Instructor

层叠样式表(CSS)

h1 {
    color: red;
}
p {
    font-style: italic;
}
<html>
  <body>
    <h1>欢迎学习网络爬虫!</h1>
    <p>无论用 R、Python,或任何其他语言,
      爬虫都很有趣!</p>
  </body>
</html>
R 语言网页抓取

CSS 选择器

h1 {
  color: green;
}
p {
  font-style: italic;
}
h1, p {
  font-family: sans-serif;
}
...
    <h1>另一个 CSS 示例。</h1>
    <p>一些文本。</p>
...

另一个 CSS 示例

html %>% html_elements('h1, p')
{xml_nodeset (2)}
[1] <h1> Another CSS ex...
[2] <p>Some text</p>
R 语言网页抓取

类型选择器

type {
    key: value;
}

html %>% html_elements('type') # 例如 'h1'、'a' 或 'span'
type1, type2 {
    key: value;
}

html %>% html_elements('type1, type2')
* {
    key: value;
}

html %>% html_elements('*')
R 语言网页抓取

开始练习!

R 语言网页抓取

Preparing Video For Download...