【问题标题】:How can I use a loop to scrape website data for multiple webpages in R?如何使用循环在 R 中为多个网页抓取网站数据?
【发布时间】:2015-03-06 03:11:09
【问题描述】:

我想应用循环从 R 中的多个网页中抓取数据。我能够为一个网页抓取数据,但是当我尝试对多个页面使用循环时,我得到了一个令人沮丧的错误。我花了几个小时修修补补,无济于事。任何帮助将不胜感激!!!

这行得通:

###########################
# GET COUNTRY DATA
###########################

library("rvest")

site <- paste("http://www.countryreports.org/country/","Norway",".htm", sep="")
site <- html(site)

stats<-
    data.frame(names =site %>% html_nodes(xpath="//*/td[1]") %>% html_text() ,
         facts =site %>% html_nodes(xpath="//*/td[2]") %>% html_text() ,
         stringsAsFactors=FALSE)

stats$country <- "Norway"
stats$names   <- gsub('[\r\n\t]', '', stats$names)
stats$facts   <- gsub('[\r\n\t]', '', stats$facts)
View(stats)

但是,当我尝试在循环中编写此代码时,我收到一个错误

###########################
# ATTEMPT IN A LOOP
###########################

country<-c("Norway","Sweden","Finland","France","Greece","Italy","Spain")

for(i in country){

site <- paste("http://www.countryreports.org/country/",country,".htm", sep="")
site <- html(site)

stats<-
data.frame(names =site %>% html_nodes(xpath="//*/td[1]") %>% html_text() ,
         facts =site %>% html_nodes(xpath="//*/td[2]") %>% html_text() ,
       stringsAsFactors=FALSE)

stats$country <- country
stats$names   <- gsub('[\r\n\t]', '', stats$names)
stats$facts   <- gsub('[\r\n\t]', '', stats$facts)

stats<-rbind(stats,stats)
stats<-stats[!duplicated(stats),]
}

错误:

Error: length(url) == 1 is not TRUE
In addition: Warning message:
In if (grepl("^http", x)) { :
  the condition has length > 1 and only the first element will be used

【问题讨论】:

  • 这里的结果相同。我尝试了这段代码,即使在有效的非循环中也得到了相同的错误消息! > 长度(站点)[1] 7 > stopifnot(长度(站点)== 1)错误:长度(站点)== 1 不正确
  • 在这一行:site &lt;- paste("http://www.countryreports.org/country/",country,".htm", sep="") 您正在使用country,在循环版本中,这是一个包含您所有国家/地区的字符向量。您可能想要i,它是您所在国家/地区向量的一个元素。
  • zelite - 这让我更接近 - 谢谢。
  • 感谢两位的帮助。我将添加最终的工作代码以供参考-希望对某人有所帮助!

标签: r loops rvest


【解决方案1】:

最终工作代码:

###########################
# THIS WORKS!!!!
###########################

country<-c("Norway","Sweden","Finland","France","Greece","Italy","Spain")

for(i in country){

site <- paste("http://www.countryreports.org/country/",i,".htm", sep="")
site <- html(site)

stats<-
data.frame(names =site %>% html_nodes(xpath="//*/td[1]") %>% html_text() ,
     facts =site %>% html_nodes(xpath="//*/td[2]") %>% html_text() ,
       stringsAsFactors=FALSE)

stats$nm <- i
stats$names   <- gsub('[\r\n\t]', '', stats$names)
stats$facts   <- gsub('[\r\n\t]', '', stats$facts)
#stats<-stats[!duplicated(stats),]
all<-rbind(all,stats)

}
 View(all)

【讨论】:

  • 这对你真的有用吗?旨在做类似的事情,因此运行您的代码并收到以下错误:rep(xi,length.out = nvar)中的错误:尝试复制“内置”类型的对象。您之前是否在某个地方启动了“全部”?
【解决方案2】:

这就是我所做的。这不是最好的解决方案,但你会得到一个输出。这也只是一种解决方法。我不建议您在运行循环时将表输出写入文件。干得好。从stats生成输出后,

output<-rbind(stats,i)

然后将表写入,

write.table(output, file = "D:\\Documents\\HTML\\Test of loop.csv", row.names = FALSE, append = TRUE, sep = ",")

#then close the loop
}

祝你好运

【讨论】:

    【解决方案3】:

    在循环之前初始化空数据框。 我已经解决了这个问题,下面的代码对我来说很好。

    country<-c("Norway","Sweden","Finland","France","Greece","Italy","Spain")
    df <- data.frame(names = character(0),facts = character(0),nm = character(0))
    
    for(i in country){
    
      site <- paste("http://www.countryreports.org/country/",i,".htm", sep="")
      site <- html(site)
    
      stats<-
        data.frame(names =site %>% html_nodes(xpath="//*/td[1]") %>% html_text() ,
                   facts =site %>% html_nodes(xpath="//*/td[2]") %>% html_text() ,
                   stringsAsFactors=FALSE)
    
      stats$nm <- i
      stats$names   <- gsub('[\r\n\t]', '', stats$names)
      stats$facts   <- gsub('[\r\n\t]', '', stats$facts)
      #stats<-stats[!duplicated(stats),]
      #all<-rbind(all,stats)
      df <- rbind(df, stats)
      #all <- merge(Output,stats)
    
    }
    View(df)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-12
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 2022-01-24
      • 2017-08-06
      • 1970-01-01
      相关资源
      最近更新 更多