溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

R語言如何獲取數(shù)據(jù)

發(fā)布時(shí)間:2022-03-28 10:23:30 來源:億速云 閱讀:626 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“R語言如何獲取數(shù)據(jù)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“R語言如何獲取數(shù)據(jù)”吧!

今天只分享數(shù)據(jù)獲取的代碼,為了顯得項(xiàng)目規(guī)范性(其實(shí)就是裝X),我第一次使用了Rstudio中的Create Projects菜單創(chuàng)建了本地項(xiàng)目倉庫(以前寫R代碼太飄逸了,寫的龍飛鳳舞,完全不顧及別人能不能看懂,以后不可以這樣了,因?yàn)楣ぷ髦幸呀?jīng)吃過很大虧了)。

R語言如何獲取數(shù)據(jù)

因?yàn)槭呛卸壛斜眄?,所以第一步的想法自然是先爬取年份鏈接,然后遍歷鏈接抓取每一年份中的文檔。

可能因?yàn)樽约何目粕季S的問題,不太習(xí)慣直接寫雙層for循環(huán)(因?yàn)榭吹綍贿m),所以遇到這種需要二次遍歷的,我一般都會拆成兩個小步驟去進(jìn)行:

1、遍歷年份對應(yīng)的對應(yīng)年政府工作報(bào)告主頁鏈接:

## !/user/bin/env RStudio 1.1.423
## -*- coding: utf-8 -*-
## Pages_links Acquisition
## 加載必要的安裝包:
library("rvest")
library("stringr")
library("Rwordseg")
library("wordcloud2")
library("dplyr")
#主網(wǎng)址
url <- "http://www.gov.cn/guowuyuan/baogao.htm"
#提取二級鏈接
txt<-read_html(url) %>%     html_nodes("#history_report") %>%     html_nodes("p") %>%     html_text()
#提取年份&鏈接信息:
Base <- read_html(url) %>% html_nodes("div.history_report") %>% html_nodes("a") Year  <- Base %>% html_text(trim = TRUE) %>% as.numeric() Links <- Base %>% html_nodes("a") %>% html_attr("href") %>% str_trim("both")
#合并成數(shù)據(jù)框:

Reports_links <- data.frame(  Year = Year,  Links = Links,  stringsAsFactors = FALSE )
#存放到本地目錄中

if (!dir.exists("data")){  dir.create("data")  write.csv(  Reports_links,  "./data/Reports_links.csv",  row.names=FALSE  )   }

R語言如何獲取數(shù)據(jù)

以上代碼為了便于理解,我都拆成單句展示了,github中代碼都會是封裝好的模塊化函數(shù)。

R語言如何獲取數(shù)據(jù)

2、從每一個年份對應(yīng)的鏈接中獲取整個政府工作報(bào)告的文檔文本:

#加載包

library("rvest")
library("dplyr")
library("magrittr")
library("doParallel")      
library("foreach")
#讀取年份及對應(yīng)鏈接

Links_data <- read.csv("./data/Reports_links.csv",stringsAsFactors = FALSE) %>% arrange(Year)
#創(chuàng)建文檔提取函數(shù):

Get_Corpus_Report <- function(i){  url = grep(i,Links_data$Year) %>% Links_data$Links[.]  read_html(url) %>%    html_nodes("td.p1,tr > td,div.pages_content") %>%    html_text("both") %>%    cat(file = sprintf("./data/Corpus/%d.txt",i))  }

以上需用到較為基礎(chǔ)的CSS表達(dá)式配色rvest來提取文檔,如果你還不太了解這塊的內(nèi)容,趕快通過菜單中的網(wǎng)絡(luò)數(shù)據(jù)獲取筆記來惡補(bǔ)。

沒有構(gòu)造循環(huán),這里用了foreach包提供的多進(jìn)程并行爬取方案來處理多循環(huán)問題(雖然這里的量級還體現(xiàn)不出來并行的優(yōu)勢,但是整體代碼要比寫循環(huán)簡介、高效)

system.time({
 if (!dir.exists("./data/Corpus")){    dir.create("./data/Corpus")  }  cl<- makeCluster(4)        registerDoParallel(cl)      tryCatch({    foreach(      i= Links_data$Year,        .combine = c,      .packages = c("rvest","magrittr")    ) %dopar% Get_Corpus_Report(i)  },  error = function(e) {    print(e)  },  finally = stopCluster(cl)  ) })

R語言如何獲取數(shù)據(jù)

R語言如何獲取數(shù)據(jù)

感謝各位的閱讀,以上就是“R語言如何獲取數(shù)據(jù)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對R語言如何獲取數(shù)據(jù)這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI