I tried to scrap from Finviz for some stock key stats. I applied codes from the original question: Web scraping of key stats in Yahoo! Finance with R. To collect stats for a
Maybe something in these lines? Trying to filter stocks before using your forloop.
library(tidyverse)
#AGM.A should produce error
stocks <- c("AXP","BA","CAT","AGM.A")
urls <- paste0("http://finviz.com/quote.ashx?t=", stocks)
#Test urls with possibly first and find out NAs
temp_ind <- map(urls, possibly(readLines, otherwise = NA_real_))
ind <- map_lgl(map(temp_ind, c(1)), is.na)
ind <- which(ind == TRUE)
filter.stocks <- stocks[-ind]
#AGM.A is removed and you can just insert stocks which work to for loop.
filter.stocks
[1] "AXP" "BA" "CAT"
As statxiong pointed out url.exist
here is simpler version:
library(RCurl)
library(tidyverse)
stocks[map_lgl(urls, url.exists)]