【问题标题】:Using rvest to scrape specific html table using the heading name使用 rvest 使用标题名称抓取特定的 html 表
【发布时间】:2020-09-13 13:51:42
【问题描述】:

试图从特定的建筑许可信息表中抓取数据。以下代码适用于我正在循环的大多数建筑许可证:

library(rvest)

permit_numbers <- c("BP125602", "BP125473", "BP125472")

URL <- paste("https://www.nanaimo.ca/WhatsBuilding/Folder", permit_numbers, sep = "/")

task_table <- lapply(URL, function(x) {
    x %>%
    read_html() %>%
    html_table() %>%
    .[[3]] %>%
    .[["Task"]]
})

但有时任务信息不在页面的第三个表中。例如,https://www.nanaimo.ca/WhatsBuilding/Folder/BP125721 任务信息在第二个表中。

如何从“任务”列标题中抓取信息,而不管它在页面上的什么位置?

【问题讨论】:

    标签: r web-scraping rvest


    【解决方案1】:

    这应该可行:

    library(rvest)
    URL <- "https://www.nanaimo.ca/WhatsBuilding/Folder/BP125602"
    
    tables <- URL %>%
      read_html() %>%
      html_table()
    
    
    task_table <- lapply(tables, function(x) if(names(x) == "Task"){x})
    
    task_table[sapply(task_table, is.null)] <- NULL
    task_table <- task_table[[1]][["Task"]]
    

    这就是你要找的吗?

    【讨论】:

    • 啊,我明白了,我在想我需要使用 html_nodes() 来识别它,但是这行得通,谢谢。我已经更新了我的原始问题以包括多个建筑许可。您介意展示如何将您的建议嵌套在现有循环中吗?似乎我应该能够插入它,但我显然错过了一些东西
    猜你喜欢
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    相关资源
    最近更新 更多