R中有好几个包都可以抓取网页数据,但是rvest + CSS Selector最方便。

通过查看器立刻知道表格数据都在td:nth-child(1),td:nth-child(3)之类的节点中,直接代码提取就行了。

library(rvest)

先看看都有什么

freak <- html_session("http://torrentfreak.com/top-10-most-pirated-movies-of-the-week-130304/")

freak

<session> http://torrentfreak.com/top-10-most-pirated-movies-of-the-week-130304/

Status: 200

Type:   text/html; charset=UTF-8

Size:   24983

freak %>% html_nodes("td:nth-child(3)") %>% html_text() %>% .[1:10]

[1] "Silver Linings Playbook "

[2] "The Hobbit: An Unexpected Journey "

[3] "Life of Pi (DVDscr/DVDrip)"

[4] "Argo (DVDscr)"

[5] "Identity Thief "

[6] "Red Dawn "

[7] "Rise Of The Guardians (DVDscr)"

[8] "Django Unchained (DVDscr)"

[9] "Lincoln (DVDscr)"

[10] "Zero Dark Thirty "

freak %>% html_nodes("td:nth-child(1)") %>% html_text() %>% .[2:11]

[1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

freak %>% html_nodes("td:nth-child(4)") %>% html_text() %>% .[1:10]

[1] "7.4 / trailer" "8.2 / trailer" "8.3 / trailer" "8.2 / trailer"

[5] "8.2 / trailer" "5.3 / trailer" "7.5 / trailer" "8.8 / trailer"

[9] "8.2 / trailer" "7.6 / trailer"

freak %>% html_nodes("td:nth-child(4) a[href*='imdb']") %>% html_attr("href") %>% .[1:10]

[1] "http://www.imdb.com/title/tt1045658/"

[2] "http://www.imdb.com/title/tt0903624/"

[3] "http://www.imdb.com/title/tt0454876/"

[4] "http://www.imdb.com/title/tt1024648/"

[5] "http://www.imdb.com/title/tt2024432/"

[6] "http://www.imdb.com/title/tt1234719/"

[7] "http://www.imdb.com/title/tt1446192/"

[8] "http://www.imdb.com/title/tt1853728/"

[9] "http://www.imdb.com/title/tt0443272/"

[10] "http://www.imdb.com/title/tt1790885/?"

#构建数据框

data.frame(movie=freak %>% html_nodes("td:nth-child(3)") %>% html_text() %>% .[1:10],

rank=freak %>% html_nodes("td:nth-child(1)") %>% html_text() %>% .[2:11],

rating=freak %>% html_nodes("td:nth-child(4)") %>% html_text() %>% .[1:10],

imdb.url=freak %>% html_nodes("td:nth child(4) a[href*='imdb']") %>% html_attr("href") %>% .[1:10],stringsAsFactors=FALSE)

movie rank        rating                              imdb.url

1            Silver Linings Playbook     1 7.4 / trailer  http://www.imdb.com/title/tt1045658/

2  The Hobbit: An Unexpected Journey     2 8.2 / trailer  http://www.imdb.com/title/tt0903624/

3          Life of Pi (DVDscr/DVDrip)    3 8.3 / trailer  http://www.imdb.com/title/tt0454876/

4                       Argo (DVDscr)    4 8.2 / trailer  http://www.imdb.com/title/tt1024648/

5                     Identity Thief     5 8.2 / trailer  http://www.imdb.com/title/tt2024432/

6                           Red Dawn     6 5.3 / trailer  http://www.imdb.com/title/tt1234719/

7      Rise Of The Guardians (DVDscr)    7 7.5 / trailer  http://www.imdb.com/title/tt1446192/

8           Django Unchained (DVDscr)    8 8.8 / trailer  http://www.imdb.com/title/tt1853728/

9                    Lincoln (DVDscr)    9 8.2 / trailer  http://www.imdb.com/title/tt0443272/

10                  Zero Dark Thirty    10 7.6 / trailer  http://www.imdb.com/title/tt1790885/?

如果不考虑网址,还有更简单的方式:

freak %>% html_nodes("table") %>% html_table()

[[1]]

Ranking (last week)                             Movie IMDb Rating / Trailer

1  torrentfreak.com        <NA>                              <NA>                  <NA>

2                 1         (5)           Silver Linings Playbook         7.4 / trailer

3                 2      (back) The Hobbit: An Unexpected Journey         8.2 / trailer

4                 3         (9)        Life of Pi (DVDscr/DVDrip)         8.3 / trailer

5                 4      (back)                     Argo (DVDscr)         8.2 / trailer

6                 5         (…)                    Identity Thief         8.2 / trailer

7                 6         (1)                          Red Dawn         5.3 / trailer

8                 7         (2)    Rise Of The Guardians (DVDscr)         7.5 / trailer

9                 8         (4)         Django Unchained (DVDscr)         8.8 / trailer

10                9         (6)                  Lincoln (DVDscr)         8.2 / trailer

11               10      (back)                  Zero Dark Thirty         7.6 / trailer

转载:R语言rvest包使用的更多相关文章

  1. R语言rvest包网络爬虫

    R语言网络爬虫初学者指南(使用rvest包) 钱亦欣 发表于 今年 06-04 14:50   5228 阅读   作者 SAURAV KAUSHIK 译者 钱亦欣 引言 网上的数据和信息无穷无尽,如 ...

  2. R语言扩展包dplyr——数据清洗和整理

    R语言扩展包dplyr——数据清洗和整理 标签: 数据R语言数据清洗数据整理 2015-01-22 18:04 7357人阅读 评论(0) 收藏 举报  分类: R Programming(11)  ...

  3. R语言-神经网络包RSNNS

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  4. R语言-Knitr包的详细使用说明

    R语言-Knitr包的详细使用说明 by 扬眉剑 来自数盟[总舵] 群:321311420 1.相关资料 1:自动化报告-谢益辉 https://github.com/yihui/r-ninja/bl ...

  5. R语言dplyr包初探

    昨天学了一下R语言dplyr包,处理数据框还是很好用的.记录一下免得我忘记了... 先写一篇入门的,以后有空再写一篇详细的用法. #dplyr learning library(dplyr) #fil ...

  6. R语言 ggplot2包

    R语言  ggplot2包的学习   分析数据要做的第一件事情,就是观察它.对于每个变量,哪些值是最常见的?值域是大是小?是否有异常观测? ggplot2图形之基本语法: ggplot2的核心理念是将 ...

  7. 安装R语言的包的方法

    安装R语言的包的方法: 1. 在线安装 在R的控制台,输入类似install.packages("TSA")  # 安装 TSA install.packages("TS ...

  8. R语言 arules包 apriori()函数中文帮助文档(中英文对照)

    apriori(arules) apriori()所属R语言包:arules                                         Mining Associations w ...

  9. Bagging(R语言实现)—包外错误率,多样性测度

    1.      Bagging Bagging即套袋法,其算法过程如下: 从原始样本集中抽取训练集.每轮从原始样本集中使用Bootstraping的方法抽取n个训练样本(在训练集中,有些样本可能被多次 ...

随机推荐

  1. java.lang.IllegalArgumentException: Service not registered

    java.lang.IllegalArgumentException: Service not registered 首先检查一下,Service是否在AndroidManifest文件中注册.格式如 ...

  2. Alberta family's QR code is world's largest corn maze

    BY DARREN WEIR     SEP 10, 2012 IN ODD NEWS Link:http://www.digitaljournal.com/article/332512   Laco ...

  3. LuoguP1226 【模板】快速幂||取余运算

    题目链接:https://www.luogu.org/problemnew/show/P1226 第一次学快速幂,将别人对快速幂原理的解释简要概括一下: 计算a^b时,直接乘的话计算次数为b,而快速幂 ...

  4. 清明梦超能力者黄YY(idx数组)

    清明梦超能力者黄YY https://www.nowcoder.com/acm/contest/206/I 题目描述 黄YY是一个清明梦超能力者,同时也是一个记忆大师.他能够轻松控制自己在梦中的一切, ...

  5. Redis数据持久化

    持久化选项 Redis提供了两种不同的持久化方法来将数据存储到硬盘里面.一种方法叫快照(snapshotting),它可以将存在于某一时刻的所有数据都写入硬盘里面.另一种方法叫只追加文件(append ...

  6. (转载)Zab vs. Paxos

    原创链接:https://cwiki.apache.org/confluence/display/ZOOKEEPER/Zab+vs.+Paxos Is Zab just a special imple ...

  7. sublime3 多行编辑.摘抄

    Sublime text 3是一个非常强大的网站编辑工具. 这里小云深深的被它的快速编辑多行内容功能所吸引. 先说下,使用下面的功能要安装一个叫emmet的插件.没有的话,自行度娘吧. 下面就来看下具 ...

  8. tty linux 打开和设置范例

    http://bbs.csdn.net/topics/340184140 /************************************************************** ...

  9. mac安全与隐私只有两个选项,少了一个任何来源

    很多软件安装后就会出现,程序已损坏,请移到废纸篓的提示. 解决方法:在终端里输入:sudo spctl --master-disable 然后回车,然后输入密码,即可在安全选项中看到重新出现的允许任何 ...

  10. 2018.08.28 洛谷P4556 [Vani有约会]雨天的尾巴(树上差分+线段树合并)

    传送门 要求维护每个点上出现次数最多的颜色. 对于每次修改,我们用树上差分的思想,然后线段树合并统计答案就行了. 注意颜色很大需要离散化. 代码: #include<bits/stdc++.h& ...