【问题标题】:Web scraping of stock key stats from Finviz with R使用 R 从 Finviz 抓取股票关键统计数据
【发布时间】:2017-11-20 20:43:46
【问题描述】:

我试图从 Finviz 报废一些股票关键数据。我应用了原始问题的代码:Web scraping of key stats in Yahoo! Finance with R。为了尽可能多地收集股票的统计数据,我创建了一个股票代码和描述列表,如下所示:

Symbol Description
A      Agilent Technologies
AAA    Alcoa Corp
AAC    Aac Holdings Inc
BABA   Alibaba Group Holding Ltd
CRM    Salesforce.Com Inc
...

我选择了第一列并将其作为一个字符存储在 R 中,并称之为股票。然后我应用了代码:

for (s in stocks) {
url <- paste0("http://finviz.com/quote.ashx?t=", s)
webpage <- readLines(url)
html <- htmlTreeParse(webpage, useInternalNodes = TRUE, asText = TRUE)
tableNodes <- getNodeSet(html, "//table")

# ASSIGN TO STOCK NAMED DFS
assign(s, readHTMLTable(tableNodes[[9]], 
                      header= c("data1", "data2", "data3", "data4", "data5", "data6",
                                "data7", "data8", "data9", "data10", "data11", "data12")))

# ADD COLUMN TO IDENTIFY STOCK 
df <- get(s)
df['stock'] <- s
assign(s, df)
}

# COMBINE ALL STOCK DATA 
stockdatalist <- cbind(mget(stocks))
stockdata <- do.call(rbind, stockdatalist)
# MOVE STOCK ID TO FIRST COLUMN
stockdata <- stockdata[, c(ncol(stockdata), 1:ncol(stockdata)-1)]

但是,对于某些股票,Finviz 没有它们的页面,我收到如下错误消息:

Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open URL 'http://finviz.com/quote.ashx?t=AGM.A': HTTP status was '404 
Not Found'

有很多股票都有这种情况,所以我无法手动将它们从我的列表中删除。有没有办法跳过获取这些股票的页面?提前致谢!

【问题讨论】:

    标签: r web-scraping finance stock


    【解决方案1】:

    也许这些行中有什么?在使用 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"
    

    正如 statxiong 指出的 url.exist 这里是更简单的版本:

    library(RCurl)
    library(tidyverse)
    
    stocks[map_lgl(urls, url.exists)]
    

    【讨论】:

    • 谢谢Hakki,这真的很有帮助!我还从 RCurl 包中发现了这个函数 url.exists,它可以确定对特定 URL 的请求是否响应没有错误
    • 我也添加了那个以备将来使用
    • 你现在关闭这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    相关资源
    最近更新 更多