网络上有大量的信息与数据。我们可以利用爬虫技术来获取这些巨大的数据资源。

这次用 IMDb 网站的2018年100部最欢迎的电影 来练练手,顺便总结一下 R 爬虫的方法。

##### >> Preparation


感谢 Hadley Wickham 大大,我们有 rvest 包可以用。因此爬虫前先安装并加载 rvest 包。

#install package
install.package('rvest')
#loading library
library('rvest')

##### >> Downloading and parsing HTML file


指定网页地址并且使用 read_html() 函数将网页转为 XML 对象。

url <- 'https://www.imdb.com/search/title?count=100&release_date=2018-01-01,2018-12-31&view=advanced'
webpage <- read_html(url)

##### >> Extracting Nodes


我期望获取的数据包括:

  1. Rank: 排名
  2. Title:电影名称
  3. Runtime:电影时长
  4. Genre:电影类型
  5. Rating:观众评分
  6. Metascore:媒体评分
  7. Description:电影简介
  8. Votes:观众投票支持的票数
  9. Gross:电影票房

使用 html_nodes() 函数可以提取 XML 对象中的元素。其中该函数利用 CSS 选择器来匹配吻合的元素。

#Using CSS selectors to extract node
rank_data_html <- html_nodes(webpage, '.text-primary')
#Converting the node to text
rank_data <- html_text(rank_data_html)
#Converting text value to numeric value
rank_data <- as.numeric(rank_data)

因为需要利用 CSS 选择器, 所以这个部分或许需要一点 HTML/CSS 的基础知识。如果不熟悉 HTML/CSS,分享一个小方法:

  1. 用浏览器(以 Chrome 为例)打开那个网页,然后按 F12 打开开发者工具
  2. 点击开发者工具左上角的箭头去选择那个需要爬取的数据
  3. 对应的那行代码就会在右侧的开发者工具被选中
  4. 对着 CSS 选择器的文档查查就知道该怎么写了

接着用类似的 Script 提取其他元素的数据。

##### >> Handling Missing Values


爬取元素后,如果仔细检查每组元素的长度,就会发现其实某些元素是有缺失值的。比如说 Metascore

metascore_data_html <- html_nodes(webpage,'.metascore')
metascore_data <- html_text(metascore_data_html)
length(metascore_data)

怎么将网页中不存在的相应值用 NA 表示呢?

这里要了解一下 html_nodehtml_nodes 的区别了。运行 ?html_node 查看帮助文档:

html_node is like [[ it always extracts exactly one element. When given a list of nodes, html_node will always return a list of the same length, the length of html_nodes might be longer or shorter.

所以简单地说,就是我们可以先提取一组没有缺失值的父级 DOM,然后从这组 DOM 中逐个提取所需的子级 DOM。

粗暴地说,上代码:

metascore_data_html <- html_node(html_nodes(webpage, '.lister-item-content'), '.metascore')
metascore_data <- html_text(metascore_data_html)
length(metascore_data)

##### >> Making a Data Frame


等所有数据都爬取完毕,就可以将其组合成 data frame 用于后续的分析了。

movies <- data.frame(
rank = rank_data,
title = title_data,
description = description_data,
runtime = runtime_data,
genre = genre_data,
rating = rating_data,
metascorre = metascore_data,
votes = votes_data,
gross = gross_data
)

##### >> Exporting CSV File


如果不想马上开始分析工作,还可以存为 csv 文件以后用。

write.csv(movies, file = file.choose(new = TRUE), row.names = FALSE)

搞定爬虫后,现在网络上已经有很多数据资源等我们用咯。

##### >> Notes


rvest 包还有其他有用的函数可以发掘一下的:

  1. html_tag(): 提取DOM 的 tag name
  2. html_attr(): 提取DOM 的 一个属性
  3. html_attrs(): 提取DOM 的 所有属性
  4. guess_encoding() and repair_encoding(): 检测编码和修复编码 (爬取中文网页的时候会用的到的~)
  5. jump_to(), follow_link(), back(), forward(): 爬取多页面网页的时候或许会用到

##### >> Sample Code


download here

Web Scraping with R: How to Fill Missing Value (爬虫:如何处理缺失值)的更多相关文章

  1. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl 1.函数调用它自身,这样就形成了一个循环,一环套一环: from urllib.request ...

  2. 首部讲Python爬虫电子书 Web Scraping with Python

    首部python爬虫的电子书2015.6pdf<web scraping with python> http://pan.baidu.com/s/1jGL625g 可直接下载 waterm ...

  3. Web Scraping with Python读书笔记及思考

    Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...

  4. [Node.js] Web Scraping with Pagination and Advanced Selectors

    When web scraping, you'll often want to get more than just one page of data. Xray supports paginatio ...

  5. <Web Scraping with Python>:Chapter 1 & 2

    <Web Scraping with Python> Chapter 1 & 2: Your First Web Scraper & Advanced HTML Parsi ...

  6. Web scraping with Python (part II) « Jean, aka Sig(gg)

    Web scraping with Python (part II) « Jean, aka Sig(gg) Web scraping with Python (part II)

  7. 《Web Scraping With Python》Chapter 2的学习笔记

    You Don't Always Need a Hammer When Michelangelo was asked how he could sculpt a work of art as mast ...

  8. Web Scraping with Python

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  9. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href 1.查找以<a>开头的所有文本,然后判断href是否在<a> ...

随机推荐

  1. IDEA下使用protobuf2(java)

    目录 一.介绍 二.特点 三.结构 四.选择版本 五.Intellij IDEA中使用Protobuf 1.下载个protoc.exe 2.编辑个.proto文件 3.将.proto文件转成Java类 ...

  2. SQLAlchemy的group_by和order_by的区别

    1.官网解释: group_by(*criterion) apply one or more GROUP BY criterion to the query and return the newly ...

  3. Intel为Google的物联网平台Brillo推出开发板Edison

    Brillo* is a solution from Google* for building connected devices. Incorporating aspects of the Andr ...

  4. C++11,控制台输出的一段小程序。

    #include <iostream> // std::cout, std::boolalpha, std::noboolalpha int main () { bool b = true ...

  5. 怎么将本地文件上传到远程git仓库

    1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点“ ...

  6. linux 【第五篇】特殊权限及定时任务

    特殊权限 [root@VM_141_154_centos ~]# ls -ld /tmp drwxrwxrwt. 8 root root 4096 Apr 5 08:11 /tmp /tmp/ 公共目 ...

  7. 【小技能】如何检索苹果APP

    有时候要临时在PC上查询一下苹果APP的信息,但是又没有安装itunes软件,那么你可以在必应里面使用类似如下的语句,例如来搜索“aboboo site:itunes.apple.com”,就可以搜索 ...

  8. 汉诺塔算法c++源代码(递归与非递归)[转]

     算法介绍: 其实算法非常简单,当盘子的个数为n时,移动的次数应等于2^n - 1(有兴趣的可以自己证明试试看).后来一位美国学者发现一种出人意料的简单方法,只要轮流进行两步操作就可以了.首先把三根柱 ...

  9. DBENV->open

    https://stuff.mit.edu/afs/sipb/project/sandbox/golem/db-3.0.55/docs/api_c/env_open.html #include < ...

  10. Linux如何查看进程等常用命令

    1.查进程    ps命令查找与进程相关的PID号:    ps a 显示现行终端机下的所有程序,包括其他用户的程序.    ps -A 显示所有程序.    ps c 列出程序时,显示每个程序真正的 ...