Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 需要帮忙清理一个大档案吗_R_Web Scraping - Fatal编程技术网

R 需要帮忙清理一个大档案吗

R 需要帮忙清理一个大档案吗,r,web-scraping,R,Web Scraping,对于一个学校的项目,我必须刮掉一个不成问题的网站。但为了让它被称为BigData,我想把整个档案(过去5年)一扫而光。url中唯一更改的是url末尾的日期,但我不知道如何编写只更改url末尾日期的脚本 我正在使用的网站是: 我需要的日期是从2015年1月1日到2020年9月24日。这段代码的第一部分我已经弄明白了,而且我能够刮去一页。我是一个使用R的初学者,我想知道是否有人可以帮助我。代码如下所示。提前谢谢 这是我到目前为止得到的,错误就在代码下面 install.packages("

对于一个学校的项目,我必须刮掉一个不成问题的网站。但为了让它被称为BigData,我想把整个档案(过去5年)一扫而光。url中唯一更改的是url末尾的日期,但我不知道如何编写只更改url末尾日期的脚本

我正在使用的网站是:

我需要的日期是从2015年1月1日到2020年9月24日。这段代码的第一部分我已经弄明白了,而且我能够刮去一页。我是一个使用R的初学者,我想知道是否有人可以帮助我。代码如下所示。提前谢谢

这是我到目前为止得到的,错误就在代码下面

install.packages("XML")
install.packages("reshape")
install.packages("robotstxt")
install.packages("Rcrawler")
install.packages("RSelenium")
install.packages("devtools")
install.packages("exifr")
install.packages("Publish")

devtools::install_github("r-lib/xml2")

library(rvest)
library(dplyr)
library(xml)
library(stringr)
library(jsonlite)
library(xml12)
library(purrr)
library(tidyr)
library(reshape)
library(XML)
library(robotstxt)
library(Rcrawler)
library(RSelenium)
library(ps)
library(devtools)
library(exifr)
library(Publish)

#Create an url object

url<-"https://www.ongelukvandaag.nl/archief/%d "

#Verify the web can be scraped

paths_allowed(paths = c(url))

#Obtain the links for every day from 2015 to 2020

map_df(2015:2020, function(i){
  page<-read_html(sprintf(url,i))
  
  data.frame(Links = html_attr(html_nodes(page, ".archief a"),"href"))
}) -> Links %>%
  
Links$Links<-paste("https://www.ongelukvandaag.nl/",Links$Links,sep = "")

#Scrape what you want from each link:
  
d<- map(Links$Links, function(x) {
    
    Z <- read_html(x)
    
    Date <- Z %>% html_nodes(".text-muted") %>% html_text(trim = TRUE) # Last update
    All_title <- Z %>% html_nodes("h2") %>% html_text(trim = TRUE) # Title
    
    return(tibble(All_title,Date))
    
  })

而且包“xml12”和“xml”在这个版本的RStudio中不起作用

看看我的代码和我的注释:

library(purrr)
library(rvest) # don't load a lot of libraries if you don't need them
url <- "https://www.ongelukvandaag.nl/archief/"
bigdata <- 
  map_dfr(
    2015:2020,
    function(year){
      year_pg <- read_html(paste0(url, year))
      list_dates <- year_pg %>% html_nodes(xpath = "//div[@class='archief']/a") %>% html_text() # in case some dates are missing
      map_dfr(
        list_dates,
        function(date) {
          pg <- read_html(paste0(url, date))
          items <- pg %>% html_nodes("div.full > div.row")
          items <- items[sapply(items, function(x) length(x %>% html_node(xpath = "./descendant::h2"))) > 0] # drop NA items
          data.frame(
            date = date,
            title = items %>% html_node(xpath = "./descendant::h2") %>% html_text(),
            update = items %>% html_node(xpath = "./descendant::h4") %>% html_text(),
            image = items %>% html_node(xpath = "./descendant::img") %>% html_attr("src") 
          )
        }
      )
    }
  )
库(purrr)
图书馆(rvest)#如果你不需要,不要加载很多图书馆
url%html\u节点(xpath=“./substant::h2”)%%>%html\u text(),
update=items%>%html\u节点(xpath=“./后代::h4”)%>%html\u文本(),
image=items%>%html\u节点(xpath=“./后代::img”)%>%html\u属性(“src”)
)
}
)
}
)

什么意思,“xml”和“xml12”不起作用?你有什么版本的R?没有
xml
(全小写)和
xml12
之类的包。我只想指出,(1)为你迭代的每个页面创建一个对象,(2)为你的read_html()创建一个故障保护包装,或者(3)保存htmls的本地缓存副本可能更安全。如果您正在抓取一个包含数百个URL的列表,那么服务器或您的连接失败/被阻止的可能性非常高,并且您可能会丢失通过map()循环在内存中收集到的所有内容。非常感谢您的帮助!现在我终于可以继续写我的论文了。如果我的回答对你有帮助的话,弗洛里瑟夫斯会接受的。
library(purrr)
library(rvest) # don't load a lot of libraries if you don't need them
url <- "https://www.ongelukvandaag.nl/archief/"
bigdata <- 
  map_dfr(
    2015:2020,
    function(year){
      year_pg <- read_html(paste0(url, year))
      list_dates <- year_pg %>% html_nodes(xpath = "//div[@class='archief']/a") %>% html_text() # in case some dates are missing
      map_dfr(
        list_dates,
        function(date) {
          pg <- read_html(paste0(url, date))
          items <- pg %>% html_nodes("div.full > div.row")
          items <- items[sapply(items, function(x) length(x %>% html_node(xpath = "./descendant::h2"))) > 0] # drop NA items
          data.frame(
            date = date,
            title = items %>% html_node(xpath = "./descendant::h2") %>% html_text(),
            update = items %>% html_node(xpath = "./descendant::h4") %>% html_text(),
            image = items %>% html_node(xpath = "./descendant::img") %>% html_attr("src") 
          )
        }
      )
    }
  )