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

前端 未结 3 1787
無奈伤痛
無奈伤痛 2021-01-19 04:50

I would like to apply a loop to scrape data from multiple webpages in R. I am able to scrape the data for one webpage, however when I attempt to use a loop for multiple page

3条回答
  •  醉梦人生
    2021-01-19 05:17

    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)
    

提交回复
热议问题