使用任何包在R中刮擦带有多个标题的表吗? (XML,rCurl,rlist htmltab,rvest等)

时间:2019-05-13 13:32:54

标签: html r xml web-scraping rvest

我正在尝试刮擦这张桌子 http://www.hko.gov.hk/cis/dailyExtract_e.htm?y=1999&m=1

这是我所有的尝试。他们都没有接近提取任何信息。我想念什么吗?


library("rvest")
library("tidyverse")
# METHOD 1
url <- "http://www.hko.gov.hk/cis/dailyExtract_e.htm?y=1999&m=1"

data <- url %>%
  read_html() %>%
  html_nodes(xpath='//*[@id="t1"]/tbody/tr[1]') %>%
  html_table()

data <- data[[1]]

# METHOD 2

library(XML)
library(RCurl)
library(rlist)
theurl <- getURL("http://www.hko.gov.hk/cis/dailyExtract_e.htm?y=1999&m=1",.opts = list(ssl.verifypeer = FALSE) )
tables <- readHTMLTable(theurl)
tables <- list.clean(tables, fun = is.null, recursive = FALSE)
n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))
tables[[which.max(n.rows)]]


# METHOD 3 
library(htmltab)
tab <- htmltab("http://www.hko.gov.hk/cis/dailyExtract_e.htm?y=1999&m=1",
               which = '//*[@id="t1"]/tbody/tr[4]',
               header = '//*[@id="t1"]/tbody/tr[3]',
               rm_nodata_cols = TRUE)

# METHOD 4
website <-read_html("http://www.hko.gov.hk/cis/dailyExtract_e.htm?y=1999&m=1")
scraped <- website %>%
  html_nodes("table") %>%
  .[(2)] %>%
  html_table(fill = TRUE) %>%
  `[[`(1) 


# METHOD 5

getHrefs <- function(node, encoding) 
  if (!is.null(xmlChildren(node)$a)) { 
    paste(xpathSApply(node, './a', xmlGetAttr, "href"), collapse = ",")
  } else {
    return(xmlValue(xmlChildren(node)$text))
  }
data <- ( readHTMLTable("http://www.hko.gov.hk/cis/dailyExtract_e.htm?y=1999&m=1", which = 1, elFun = getHrefs)  )


预期结果应为表中的12个别名以及下面的数据

0 个答案:

没有答案