Web scraping of stock key stats from Finviz with R

后端 未结 1 1687
南笙
南笙 2021-01-16 23:32

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

相关标签:
1条回答
  • 2021-01-16 23:58

    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)]
    
    0 讨论(0)
提交回复
热议问题