【问题标题】:Scraping financial data from Yahoo Finance从 Yahoo Finance 抓取财务数据
【发布时间】:2019-10-27 19:36:15
【问题描述】:

我一直在尝试使用 R 从 Yahoo Finance 抓取财务数据,但未能成功。您可以在下面看到我当前的代码。主要问题似乎是在 Yahoo Finance 中存储财务数据的表没有被建模为 HTML 代码中的表。如何绕过这个问题?

我已经尝试复制似乎包含该表的 Xpath,但没有成功。

library(XML)

symbol = "HD"
url <- paste('https://finance.yahoo.com/quote/HD/financials?p=',symbol,sep="")
webpage <- readLines(url)
html <- htmlTreeParse(webpage, useInternalNodes = TRUE, asText = TRUE)
tableNodes <- getNodeSet(html, "//table")

data <- readHTMLTable(tableNodes)

【问题讨论】:

    标签: r web-scraping


    【解决方案1】:

    我曾经与 yahoo Finance 合作,您犯了一个小错误,因为 tableNodes 可以包含多个表格,因此请使用以下表格获取所有表格:

    library(XML)
    
    symbol = "HD"
    url <- paste('https://finance.yahoo.com/quote/HD/analysts?p=',symbol,sep="")
    webpage <- readLines(url)
    html <- htmlTreeParse(webpage, useInternalNodes = TRUE, asText = TRUE)
    tableNodes <- getNodeSet(html, "//table")
    
    earningsEstimates <- readHTMLTable(tableNodes[[1]])
    revenueEstimates <- readHTMLTable(tableNodes[[2]])
    earningsHistory <- readHTMLTable(tableNodes[[3]])
    earningPerShareTrend <- readHTMLTable(tableNodes[[4]])
    earningPerShareRevision <- readHTMLTable(tableNodes[[5]])
    growthEstimates <- readHTMLTable(tableNodes[[6]])
    
    print(earningsEstimates) # printing one table
    

    输出

     Earnings Estimate Current Qtr. (Oct 2019) Next Qtr. (Jan 2020) Current Year (2020)
    1   No. of Analysts                      28                   28                  35
    2     Avg. Estimate                    2.52                 2.17               10.13
    3      Low Estimate                    2.47                 2.07               10.03
    4     High Estimate                    2.58                 2.24               10.27
    5      Year Ago EPS                    2.51                 2.25                9.89
      Next Year (2021)
    1               35
    2            10.96
    3             10.7
    4             11.2
    5            10.13
    

    【讨论】:

    • 您好,感谢您的回答。但是,我对 Yahoo Finance 的 Analyst 部分不感兴趣,而是对 Finances 部分感兴趣,我认为 html 元素不称为 table,而是
      。我该如何更改我的代码来解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多