R语言 Web数据

2017-11-13  本文已影响0人  yuanyb

许多网站提供数据供其用户使用。 例如,世界卫生组织(WHO)以CSV,txt和XML文件的形式提供健康和医疗信息的报告。 使用R语言程序,我们可以从这些网站以编程方式提取特定数据。 R语言中用于从网站中提取数据的一些包是“RCurl”,XML“和”stringr“,它们用于连接到URL,识别文件所需的链接并将它们下载到本地环境。

安装R语言的包

处理URL和链接到文件需要以下的包。 如果它们在R语言环境中不可用,您可以使用以下命令安装它们。

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">install.packages("RCurl")
install.packages("XML")
install.packages("stringr")
install.packages("plyr")
</pre>

输入数据

我们将访问URL天气数据,并使用R在2015年下载CSV文件。

我们将使用函数getHTMLLinks()来收集文件的URL。 然后我们将使用函数downlaod.file()将文件保存到本地系统。 由于我们将对多个文件一次又一次地应用相同的代码,因此我们将创建一个被多次调用的函数。 文件名作为参数以R列表对象的形式传递到此函数。

<pre class="prettyprint notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Read the URL.
url <- "http://www.geos.ed.ac.uk/~weather/jcmb_ws/"

Gather the html links present in the webpage.

links <- getHTMLLinks(url)

Identify only the links which point to the JCMB 2015 files.

filenames <- links[str_detect(links, "JCMB_2015")]

Store the file names as a list.

filenames_list <- as.list(filenames)

Create a function to download the files by passing the URL and filename list.

downloadcsv <- function (mainurl,filename) {
filedetails <- str_c(mainurl,filename)
download.file(filedetails,filename)
}

Now apply the l_ply function and save the files into the current R working directory.

l_ply(filenames,downloadcsv,mainurl = "http://www.geos.ed.ac.uk/~weather/jcmb_ws/")
</pre>

验证文件下载

运行上述代码后,您可以在当前R语言工作目录中找到以下文件。

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"JCMB_2015.csv" "JCMB_2015_Apr.csv" "JCMB_2015_Feb.csv" "JCMB_2015_Jan.csv"
"JCMB_2015_Mar.csv"</pre>

上一篇下一篇

猜你喜欢

热点阅读