【问题标题】:Reading table from https webpage using readHTMLTable使用 readHTMLTable 从 https 网页读取表格
【发布时间】:2016-08-20 19:55:29
【问题描述】:

我已安装 R 3.3.1,并且正在使用 RStudio 0.99.903。我正在尝试从以下 URL 将表格读入 R:https://www.fantasypros.com/nfl/rankings/consensus-cheatsheets.php

(我很清楚有一个下载按钮,但是,我现在不能选择)

去年我可以使用 readHTMLTable 函数轻松地做到这一点。但是,在那个时候,该站点从使用 http 更改为 https,这会导致“XML 内容不是 XML”错误。

我尝试了这里的建议:get url table into a `data.frame` R-XML-RCurl

library(XML)
library(RCurl)
url <- getURL("https://www.fantasypros.com/nfl/rankings/consensus-cheatsheets.php")
df <- readHTMLTable(URL, header = T)

get URL 函数返回一个对我来说基本上没有意义的大字符串,这意味着 readHTMLTable 不能正常工作(我得到一个列表,有几个数据框,但这些对我来说也没有意义。它是西班牙语对我不知道它们来自哪里的事物进行观察):

>url
[1] "\r\n<!DOCTYPE html>\n<html lang=\"en\">\n\n<head>\n    <title>2016 QB Fantasy Football Rankings, QB Cheat Sheets, QB Draft / Draft Rankings</title>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n    <meta name=\"description\" content=\"Don&#8217;t trust any 1 fantasy football expert? We combine their rankings into 1 Expert Consensus Ranking. Our 2016 Draft QB rankings are updated daily.\">\n<link rel=\"canonical\" href=\"https://www.fantasypros.com/nfl/rankings/qb-cheatsheets.php\" />\n\n    <meta property=\"fb:pages\" content=\"184352014941166\"/>\n

它还有很多方法。

有人可以给我一些关于如何让它工作的建议吗?

谢谢。

【问题讨论】:

  • 这已经过去了将近一年,但是您的代码现在适用于我在 R 版本 3.4.0 Patched (2017-05-02 r72649) 下:Windows 10 x64 (build 14393) using RCurl 1.95-4.8 , bitops 1.0-6 和 XML 3.98-1.7 除了问题URL的第4行代码应该是url与第三行保持一致。

标签: r


【解决方案1】:

从 URL 获取 html 文件

library("httr")
library("XML")
URL <- "https://www.fantasypros.com/nfl/rankings/consensus-cheatsheets.php"
temp <- tempfile(fileext = ".html")
GET(url = URL, user_agent("Mozilla/5.0"), write_disk(temp))

解析 HTML 文件

doc <- htmlParse(temp)

XPath 查询是通过选择具有class = "player-table"table 元素及其具有class = 'mpb-player-' 的子tr 元素来构造的

xpexpr <- "//table[contains(@class, 'player-table')]/tbody/tr[contains(@class, 'mpb-player-')]"

从 doc 中获取 xpath 表达式的节点列表

listofTableNodes <- getNodeSet(doc, xpexpr)
listofTableNodes

使用节点列表的 xmlvalues 创建一个数据框

df <- xmlToDataFrame(listofTableNodes, stringsAsFactors = FALSE)
# alternatively xpathSApply can be used to get the same data frame
# df <- xmlToDataFrame(xpathSApply(doc, xpexpr), stringsAsFactors = FALSE)

删除空列

df <- df[, seq(1, length(df), by = 2)]

添加列名

xpexpr <- "//table[contains(@class, 'player-table')]/thead/tr/th"
listofTableNodes <- getNodeSet(doc, xpexpr)
listofTableNodes
colnames(df) <- gsub("[\r\n ]*$", '', xmlSApply(listofTableNodes, xmlValue))

head(df)
#   Rank          Player (Team) Pos Bye Best Worst Avg Std Dev ADP vs. ADP
# 1    1     Antonio Brown PIT  WR1   8    1     5 1.3     0.8 1.0     0.0
# 2    2 Odell Beckham Jr. NYG  WR2   8    1     9 3.1     1.6 2.0     0.0
# 3    3       Julio Jones ATL  WR3  11    1     6 3.4     1.1 4.0    +1.0
# 4    4        Todd Gurley LA  RB1   8    1    11 4.5     2.3 3.0    -1.0
# 5    5     David Johnson ARI  RB2   9    1    19 6.1     3.5 6.0    +1.0
# 6    6   Adrian Peterson MIN  RB3   6    1    22 7.6     3.8 5.0    -1.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 2022-11-20
    • 2016-10-19
    • 2021-11-29
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多