【问题标题】:R extracting a table from a webpageR从网页中提取表格
【发布时间】:2015-01-19 18:37:42
【问题描述】:

我想从

中提取“Match match by list”表
http://stats.espncricinfo.com/ci/engine/player/50710.html?class=2;template=results;type=batting;view=match

我是 R 新手,所以不太了解从网页中提取数据。我使用此代码来提取表格。

fileUrl<- "http://stats.espncricinfo.com/ci/engine/player/50710.html?class=2;template=results;type=batting;view=match"
#load
sanga <-htmlTreeParse(fileUrl, useInternal=T)
sanga.data <-xpathSApply(sanga,"//tr[@class='data1']",xmlValue)

但是,我最终得到一个单列矩阵,其中原始表中的每一列都表示为一行。我阅读了此线程中的信息,但仍然无法弄清楚如何以表格格式获取数据。 Scraping html tables into R data frames using the XML package

【问题讨论】:

  • XML 包有一个readHTMLTable 函数。您可以使用readHTMLTable(sanga, which = 50)readHTMLTable(sanga)$"Match by match list"

标签: r web-scraping


【解决方案1】:

您需要对列名进行一些处理(并删除 NA 'spacer' 列),但使用正确的 XPath 可以直接访问所需的表:

library(rvest)
library(magrittr)

pg <- html("http://stats.espncricinfo.com/ci/engine/player/50710.html?class=2;template=results;type=batting;view=match")

pg %>% 
  html_nodes(xpath="//tr[@class='data1']/../..") %>%  # get to a reasonable set of tables (there are many)
  extract2(2) %>%                                     # we want the second one
  html_table(header=TRUE, trim=TRUE) -> data          # there's a header and pls trim the blanks

str(data)
## data.frame':  397 obs. of  11 variables:
##  $ Bat1      : chr  "35" "85" "36*" "DNB" ...
##  $ Runs      : chr  "35" "85" "36" "-" ...
##  $ BF        : chr  "55" "116" "47" "-" ...
##  $ SR        : chr  "63.63" "73.27" "76.59" "-" ...
##  $ 4s        : chr  "4" "11" "3" "-" ...
##  $ 6s        : chr  "0" "0" "0" "-" ...
##  $           : logi  NA NA NA NA NA NA ...
##  $ Opposition: chr  "v Pakistan" "v South Africa" "v Pakistan" "v South Africa" ...
##  $ Ground    : chr  "Galle" "Galle" "Colombo (RPS)" "Colombo (SSC)" ...
##  $ Start Date: chr  "5 Jul 2000" "6 Jul 2000" "9 Jul 2000" "11 Jul 2000" ...
##  $           : chr  "ODI # 1603" "ODI # 1604" "ODI # 1608" "ODI # 1610" ...

【讨论】:

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