从R中的网页中抓取多个表格

Ana*_*dey 1 screen-scraping r data.table

我正在尝试将共同基金数据提取到 R 中,我的代码方式适用于单个表,但是当网页中有多个表时,它不起作用。

链接 - https://in.finance.yahoo.com/q/pm?s=115748.BO

我的代码

url <- "https://in.finance.yahoo.com/q/pm?s=115748.BO"
library(XML)
perftable <- readHTMLTable(url, header = T, which = 1, stringsAsFactors = F)
Run Code Online (Sandbox Code Playgroud)

但我收到一条错误消息。

(function (classes, fdef, mtable) 中的错误:无法为签名 '"NULL"' 的函数 'readHTMLTable' 找到继承的方法另外:警告消息:XML 内容似乎不是 XML:' https:// in.finance.yahoo.com/q/pm?s=115748.BO '

我的问题是

  1. 如何从此网页中拉出特定表格?
  2. 如何从该网页中拉出所有表格?
  3. 当有多个链接时,从每个网页中提取特定表格的简单方法是什么

Ahttps://in.finance.yahoo.com/q/pm?s=115748.BO

Ahttps://in.finance.yahoo.com/q/pm?s=115749.BO

Ahttps://in.finance.yahoo.com/q/pm?s=115750.BO

使用链接时,从链接中删除“A”。

jdh*_*son 5

Base R 无法访问https. 您可以使用像RCurl. 表上的标题实际上是单独的表。该页面实际上由 30 多个表组成。你想要的数据最像表给出的class = yfnc_datamodoutline1

url <- "https://in.finance.yahoo.com/q/pm?s=115748.BO"
library(XML)
library(RCurl)
appData <- getURL(url, ssl.verifypeer = FALSE)
doc <- htmlParse(appData)
appData <- doc['//table[@class="yfnc_datamodoutline1"]']
perftable <- readHTMLTable(appData[[1]], stringsAsFactors = F)
> perftable
V1      V2
1            Morningstar Return Rating:    2.00
2                  Year-to-Date Return:   2.77%
3                5-Year Average Return:   9.76%
4                   Number of Years Up:       4
5                 Number of Years Down:       1
6  Best 1 Yr Total Return (2014-12-31):  37.05%
7 Worst 1 Yr Total Return (2011-12-31): -27.26%
8         Best 3-Yr Total Return (N/A):  23.11%
9        Worst 3-Yr Total Return (N/A):  -0.33%
Run Code Online (Sandbox Code Playgroud)