How can I use a loop to scrape website data for multiple webpages in R?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 20:41:36

Final working code:

###########################
# 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)

Just initalize empty dataframe before loop. I have done this problem and following code works fine for me.

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)
SKD

This is what I did. It is not the best solution, but you will get an output. Also this is only a workaround. I do not recommend you write a table output into a file while running a loop. Here you go. After the output is generated from stats,

output<-rbind(stats,i)

and then write the table to,

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

#then close the loop
}

Good luck

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!