R自动数据收集第一章概述——《List of World Heritage in Danger》
library(stringr)
library(XML)
library(maps)
heritage_parsed <- htmlParse("http://en.wikipedia.org/wiki/List_of_World_Heritage_in_Danger",encoding = "UTF-8")
出现错误
Error: failed to load external entity "
page 2
Credit: Suryapratim Sarkar (2015-06-25)
Wikipedia changed its server communication from HTTP to HTTPS. As a result, the following lines on page 2 return an error:(的确,如果你直接粘贴原http协议的地址,浏览器的地址栏会自动帮你转成https)
heritage_parsed <- htmlParse("http://en.wikipedia.org/wiki/List_of_World_Heritage_in_Danger",
encoding = "UTF-8")
## Error: failed to load external entity "http://en.wikipedia.org/wiki/List_of_World_Heritage_in_Danger"
There are at least two solutions to the problem:
Use
getURL()
and specify the location of CA signatures (see Section 9.1.7 of our book).Use Hadley Wickham’s
rvest
package, which came out after our book was published. It facilitates scraping with R considerably, in particular in such scenarios(情形、脚本). In this specific example, use the following code instead:
library(rvest) # the new package, version 0.3.0
heritage_parsed <- read_html("http://en.wikipedia.org/wiki/List_of_World_Heritage_in_Danger", encoding = "UTF-8") # read_html() from the rvest package is the new htmlParse() from the XML package
tables <- html_table(heritage_parsed, fill = TRUE) # html_table() from the rvest package, which replaces readHTMLTable() from the XML package
From thereon, the rest of the chapter code should work. If you want to learn more about the rvest package, have a look here. We are planning to cover it extensively in the next edition of our book.

heritage_parsed <- read_html(
"http://en.wikipedia.org/wiki/List_of_World_Heritage_in_Danger", encoding = "UTF-8")
# str(heritage_parsed)
#我曾经尝试将heritage_parsed写到本地文件,但是失败了
#无法将list通过cat()输入到本地,查阅了sof,但是还是没有解决
#从heritage_parsed中取出所有的表格
tables <- html_table(heritage_parsed, fill = TRUE)
#所以,我怎么知道这是第二个表格呢?
danger_table <- tables[[2]]
#查看表格的列名
names(danger_table)
# [1] "Name" "Image" "Location"
# [4] "Criteria" "Area\nha (acre)" "Year (WHS)"
# [7] "Endangered" "Reason" "Refs"
取出表格中的1, 3, 4, 6, 7列,其含义分别是名字、位置、标准分类、申遗成功时间、何时定为处于危险的时间
#感兴趣的只有表格中的1, 3, 4, 6, 7列
#名字、位置、标准分类、申遗成功时间、何时定为处于危险的时间
#覆盖掉原来的表格
danger_table <- danger_table[, c(1, 3, 4, 6, 7)]
#为了更方便,对列重命名,
colnames(danger_table) <- c("name", "locn", "crit", "yins", "yend")
#取几个遗产的名字查看下
danger_table$name[1:3]
#str_detect是stringr包中的函数
#用于在前一个参数中按照后一个参数的规则查找是否存在特定字符
#返回值是逻辑值向量
#对crit列按照自然和人文遗产重新编码
#Natural景观遗产编码为nat,否则为culture,即cul
danger_table$crit <- ifelse(
str_detect(danger_table$crit, "Natural") ==TRUE,
"nat",
"cult"
)
fruit <- c("apple", "banana", "pear", "pinapple")
str_detect(fruit, "a")
# [1] TRUE TRUE TRUE TRUE
# 将申遗成功时间转为数值类型
danger_table$yins <- as.numeric(danger_table$yins)
#按照正则表达式取出(我们只取出濒危时间,去掉横杠,并转化为数值型)
yend_clean <- unlist(
str_extract_all(danger_table$yend, "[[:digit:]]4$")
)
yend_clean1 <- unlist(str_extract_all(danger_table$yend, "\\d{4}-"))
yend_clean <- unlist(str_extract_all(yend_clean1, "\\d{4}"))
length(yend_clean)
length(danger_table$yend)
danger_table$yend <- as.numeric(yend_clean)
danger_table$yend[1:3]
#[1] 2001 1992 2013
shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2")
str_extract(shopping_list, "\\d")
# [1] "4" NA NA "2"
str_extract(shopping_list, "[a-z]+")
# [1] "apples" "bag" "bag" "milk"
str_extract_all(shopping_list, "[a-z]+")
# [[1]]
# [1] "apples" "x"
#
# [[2]]
# [1] "bag" "of" "flour"
#
# [[3]]
# [1] "bag" "of" "sugar"
#
# [[4]]
# [1] "milk" "x"
danger_table$locn[c(1, 3, 5)]
[1] "EgyAbusir, Egypt30°50′30″N 29°39′50″E / 30.84167°N 29.66389°E / 30.84167; 29.66389 (Abu Mena)"
[2] "Syria !Aleppo Governorate, Syria36°14′0″N 37°10′0″E / 36.23333°N 37.16667°E / 36.23333; 37.16667 (Ancient City of Aleppo)"
[3] "Syria !Damascus Governorate, Syria33°30′41″N 36°18′23″E / 33.51139°N 36.30639°E / 33.51139; 36.30639 (Ancient City of Damascus)"


reg_y <- "[/][ -]*[[:digit:]]*[.]*[[:digit:]]*[;]"
reg_x <- "[;][ -]*[[:digit:]]*[.]*[[:digit:]]*"
第一段:以反斜杠/开头,或者以空格和横杠-开头,
y_coords <- str_extract(danger_table$locn, reg_y)
y_coords
[1] "/ 30.84167;" "/ 18.283;" "/ 36.23333;"
[4] "/ 32.51806;" "/ 33.51139;" "/ 36.33417;"
[7] "/ 32.82500;" "/ 32.63833;" "/ 32.80528;"
[10] "/ 35.45667;" "/ 42.26222;" "/ 17.317;"
[13] "/ -8.11111;" "/ 31.70444;" "/ 9.167;"
[16] "/ 11.417;" "/ 34.78167;" "/ 34.83194;"
[19] "/ -11.68306;" "/ 25.317;" "/ 9.55389;"
[22] "/ 4.000;" "/ 35.58806000;" "/ 39.05000;"
[25] "/ 14.200;" "/ -20.20833;" "/ -2.500;"
[28] "/ 53.40667;" "/ 9.000;" "/ 34.39667;"
[31] "/ 42.66111;" "/ 7.600;" "/ 6.83972;"
[34] "/ 13.000;" "/ 2.000;" "/ 31.77667;"
[37] "/ 15.35556;" "/ 30.13333;" "/ 13.90639;"
[40] "/ 15.92694000;" "/ -14.467;" "/ 15.74444;"
[43] "/ 24.83333;" "/ -8.95778;" "/ -2.000;"
[46] "/ 34.200;" "/ 13.183;" "/ 34.55417;"
[49] "/ 16.77333;" "/ 16.2893333;" "/ 0.32917;"
[52] "/ -2.500;" "/ 0.917;" "/ 31.71972;"
负纬度表示位于南半球(S)的位置而负经度表示西半球(W)的位置.
y_coords <- as.numeric(str_sub(y_coords, 3, -2))
danger_table$y_coords <- y_coords
x_coords <- str_extract(danger_table$locn, reg_x)
x_coords <- as.numeric(str_sub(x_coords, 3, -1))
danger_table$x_coords <- x_coords
danger_table$locn <- NULL
names(danger_table)
# [1] "name" "crit" "yins" "yend" "y_coords"
# [6] "x_coords"
round(danger_table$y_coords, 2)[1:3]
round(danger_table$x_coords, 2)[1:3]
round(danger_table$y_coords, 2)的作用是取小数点后两位,这里只是起到了查看的作用,并没有影响表中的数据
#查看维度
dim(danger_table)
head(danger_table)
#对人文和自然景观设置不同的点的形状
pch <- ifelse(danger_table$crit == "nat", 10, 11)
#对分属人文和自然景观的遗产设置不同的颜色参数
cols <- ifelse(danger_table$crit == "nat", 'deepskyblue4', 'brown1')
#嗯,输入到本地文件,免得RStudio的绘图窗口太小
#注意宽高的调整
png("d:\\map.png",width = 1300, height = 720)
map("world", col = "maroon",
lwd = 0.5,
mar = c(0.1, 0.1, 0.1, 0.1),
bg='seashell')
points(danger_table$x_coords,
danger_table$y_coords,
pch = pch,
col=cols,
cex=1.2,lwd=1.3
)
#添加图例
#leg.txt中设置的是图例文本
leg.txt <- c("Natural", "Cultural")
#当取x=0,y=0的时候是位于图正中央
#text.col设置图例文字颜色
#cex设置图例区域大小
legend("topright", leg.txt, horiz = TRUE,
pch=c(10, 11),
col = c('deepskyblue4', 'brown1'),
text.col=c('deepskyblue4', 'brown1'),
cex =2.3)
#box是边框,其样式lty的参数有点意思哈
box(lty='1373', lwd =5,col = 'red')
#关闭输出设备
dev.off()

table(danger_table$crit)
#
# cult nat
# 37 17
library(RColorBrewer)
cols1=brewer.pal(8,'Set3')
par(bg='lightcyan')
hist(danger_table$yend,
freq = TRUE,
xlab = "Year when site was put on the list of endangered sites",
col=cols1,border = cols1,
main = "")

#奇怪的样式
cols2=brewer.pal(8,'Paired')
par(bg='lightcyan')
hist(
danger_table$yend,
freq = TRUE,density = TRUE,
xlab = "Year when site was put on the list of endangered sites",
col=cols2,main = "")

唉呀,我岂不是可以用来抓取淘宝的商品评论,进行评价分析?
R自动数据收集第一章概述——《List of World Heritage in Danger》的更多相关文章
- R自动数据收集第二章HTML笔记1(主要关于handler处理器函数和帮助文档所有示例)
本文知识点: 1潜在畸形页面使用htmlTreeParse函数 2startElement的用法 3闭包 4handler函数的命令和函数体主要写法 5节点的丢弃,取出,取出标签名称.属性.属 ...
- R自动数据收集第二章HTML笔记2(主要关于htmlTreeParse函数)
包含以下几个小的知识点 1htmlTreeParse函数源码和一些参数 2hander的写法 3关于missing函数 4关于if-else语句中else语句的花括号问题 5关于checkHandle ...
- 【译文连载】 理解Istio服务网格(第一章 概述)
书籍英文版下载链接为 https://developers.redhat.com/books/introducing-istio-service-mesh-microservices/,作者 Burr ...
- TCP/IP详解 卷1 第一章概述
第一章概述 1.2 分层 网络编程通常分不同层次进行开发,每一层负责不同的通信功能. 一个协议族比如TCP/IP,通常是一组不同层次上多个协议的组合.一般可以认为是是四层协议系统: 链路层:有时也称作 ...
- perl5 第一章 概述
第一章 概述 by flamephoenix 一.Perl是什么?二.Perl在哪里?三.运行四.注释 一.Perl是什么? Perl是Practical Extraction and Re ...
- 计算机网络课程优秀备考PPT之第一章概述(一)
为了记录自己从2016.9~2017.1的<计算机网络>助教生涯,也为了及时梳理和整写笔记! 以上,是<计算机网络>课程的第一章概述.
- CentOS6安装各种大数据软件 第一章:各个软件版本介绍
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- Camel In Action 阅读笔记 第一部分概述 + 第一章概述 认识Camel
第一部分: 最开始的一小步 Apache Camel 是一个开源集成框架,其目的是让系统集成变得更加简便,在本书的第一章中,我们会为您介绍它并向您展示它是如何在大型企业应用中做好集成工作.您也会了解到 ...
- htt p第一章概述
http的概述 1 web客户端与服务器是如何通信 2 web资源来自的何方 3 web事务是怎样的工作的 4 http通信所使用的报文结构 5 底层tcp的传输的结构 6不同的http协议体 什么是 ...
随机推荐
- 【推荐】CentOS安装Subversion-1.8.11+HTTP协议支持配置
注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. 我们需要搭建一个自己的SVN服务器. 此外,搭建好的SVN服务器除了需要支持svn协议外,最好还需要支持HTTP协议和HTTPS协 ...
- ASP.NET中将导出的数据以UTF-8编码方式进行存储
Response.Charset = "UTF-8"; Response.ContentEncoding = Encoding.UTF8; Response.AppendHea ...
- 《Google软件测试之道》基础
<Google软件测试之道>,一直听朋友讲起这本书,出于琐事太多,一直没机会拜读,最近部门架构觉得我们IT部门的技术太low,就给我们挑选了一些书籍,让我们多看看... 个人的一种学习习惯 ...
- 前端自动化测试 —— TDD环境配置(React+TypeScript)
欢迎讨论与指导:) 前言 TDD -- Test-Drive Development是测试驱动开发的意思,是敏捷开发中的一项核心实践和技术,也是一种测试方法论.TDD的原理是在开发功能代码之前,先编写 ...
- EF里一对一、一对多、多对多关系的配置和级联删除
本章节开始了解EF的各种关系.如果你对EF里实体间的各种关系还不是很熟悉,可以看看我的思路,能帮你更快的理解. I.实体间一对一的关系 添加一个PersonPhoto类,表示用户照片类 /// < ...
- 如何用卷积神经网络CNN识别手写数字集?
前几天用CNN识别手写数字集,后来看到kaggle上有一个比赛是识别手写数字集的,已经进行了一年多了,目前有1179个有效提交,最高的是100%,我做了一下,用keras做的,一开始用最简单的MLP, ...
- ECharts学习(3)--toolbox(工具栏)
1. toolbox:这是ECharts中的工具栏.内置有导出图片.数据视图.动态类型切换.数据区域缩放.重置五个工具. 2. toolbox中的属性,不包含五个工具.里面最主要的就是feature这 ...
- 小猪cms命名规则整理
1.模板命名规则: 类名+函数名称+.html 2.Model命名规则: 数据库表名:cms_user_data 文件名称命名: UserDataModel.class.php <?php cl ...
- java第三周学习
这一周学习的是java数组面向对象 数组中存放的数据的类型:既可以是基本数据类型也可以是引用数据类型. 数组的定义方式: 1 数据类型[] 数组名; 2 数据类型 数组名[]; 数组的初始化: 1.静 ...
- zrt中文题
orzzrt.... 题意:给n个点n条边,问能形成几个无向连通图公式:ans=Σ(k=3~n){[n^(n-k)]* (n-1)!/2(n-k)!}推导:ans=Σ(k=3~n)(f(n,k)*h( ...