0

I'm trying to download wikipedia pages for a list of names. Not all the names on the list have a wiki page though. I want to loop through the list, save the pages that exist and skip past the names that do not. Is there a way to do this?

Here's where I got to:

library('WikipediR')

names_dat = data.frame(name = c('Donald Trump', 'Jo Biden', 'Fakey McNotreal', 'Bernie Sanders')

wiki_list = list()

for (i in 1:nrow(names_dat)){
  temp = page_content(domain = 'wikipedia.org',
                      page_name = names_dat$name[i],
                      as_wikitext = F)
  wiki_list[[i]] = temp
}

Thanks!

mendy
  • 329
  • 1
  • 9
  • 1
    You probably want `try`. https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/try. or `tryCatch` :https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r – QAsena Jan 08 '21 at 11:31
  • 1
    You are looking for try catch .. https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r – Amit Jan 08 '21 at 11:32

1 Answers1

1

See comments as potential duplicate but I think this is what you are after:

library('WikipediR')

names_dat = data.frame(name = c('Donald Trump', 'Jo Biden', 'Fakey McNotreal', 'Bernie Sanders'))
                       
wiki_list = list()

for (i in 1:nrow(names_dat)){
  tryCatch({
    temp = page_content(domain = 'wikipedia.org',
                        page_name = names_dat$name[i],
                        as_wikitext = F)
    wiki_list[[i]] = temp
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})

}

QAsena
  • 603
  • 4
  • 9