【问题标题】:Inputting NA where there are missing values when scraping with rvest使用 rvest 抓取时在缺失值的地方输入 NA
【发布时间】:2017-08-27 03:51:11
【问题描述】:

我想使用rvest 来抓取一个页面,其中包含最近一次会议上的演讲标题和运行时间,然后将这些值组合成一个tibble

library(tibble)
library(rvest)

url <- "https://channel9.msdn.com/Events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference?sort=status&direction=desc&page=14"

title <- page %>% 
      html_nodes("h3 a") %>% 
      html_text()

length <- page %>% 
      html_nodes(".tile .caption") %>% 
      html_text()

df <- tibble(title,length)

如果您查看该页面,您会发现其中一个演讲没有任何价值 - 在查看源代码中,此演讲没有 class="caption"

有什么方法可以替换 NA 来显示缺失值?

【问题讨论】:

    标签: r rvest tibble


    【解决方案1】:

    最简单的方法是为每一行选择一个包含您想要的两个节点的节点,然后遍历它们,同时拉出您想要的两个节点。 purrr::map_df 不仅可以方便地进行迭代,甚至可以将结果组合成一个漂亮的小标题:

    library(rvest)
    library(purrr)
    
    url <- "https://channel9.msdn.com/Events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference?sort=status&direction=desc&page=14"
    
    page <- read_html(url)
    
    df <- page %>% 
        html_nodes('article') %>%    # select enclosing nodes
        # iterate over each, pulling out desired parts and coerce to data.frame
        map_df(~list(title = html_nodes(.x, 'h3 a') %>% 
                         html_text() %>% 
                         {if(length(.) == 0) NA else .},    # replace length-0 elements with NA
                     length = html_nodes(.x, '.tile .caption') %>% 
                         html_text() %>% 
                         {if(length(.) == 0) NA else .}))
    
    df
    #> # A tibble: 12 x 2
    #>                                                                                title   length
    #>                                                                                <chr>    <chr>
    #>  1                             Introduction to Natural Language Processing with R II 01:15:00
    #>  2                                Introduction to Natural Language Processing with R 01:22:13
    #>  3                                          Solving iteration problems with purrr II 01:22:49
    #>  4                                             Solving iteration problems with purrr 01:32:23
    #>  5                           Markov-Switching GARCH Models in R: The MSGARCH Package    15:55
    #>  6                    Interactive bullwhip effect exploration using SCperf and Shiny    16:02
    #>  7                             Actuarial and statistical aspects of reinsurance in R    14:15
    #>  8                                                            Transformation Forests    16:19
    #>  9                                                         Room 2.02 Lightning Talks    50:35
    #> 10                                   R and Haskell: Combining the best of two worlds    14:45
    #> 11 *GNU R* on a Programmable Logic Controller (PLC) in an Embedded-Linux Environment     <NA>
    #> 12     Performance Benchmarking of the R Programming Environment on Knight's Landing    19:32
    

    【讨论】:

    • 很好的答案,谢谢!我真的很困惑是否也可以使用 xpath 标识符。评论可能太长了,所以我发布了一个新问题。再次感谢,从您的帖子中学到了很多东西!
    【解决方案2】:

    我遇到了同样的问题,但我设法使它工作,而不涉及包含两个指定节点的节点。

    使用您的代码将是:

    library(tibble)
    library(rvest)
    
    url <- "https://channel9.msdn.com/Events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference?sort=status&direction=desc&page=14"
    
    title <- page %>% 
          html_nodes("h3 a") %>% 
          html_text() %>% 
                         {if(length(.) == 0) NA else .
    
    length <- page %>% 
          html_nodes(".tile .caption") %>% 
          html_text() %>% 
                         {if(length(.) == 0) NA else .
    
    df <- tibble(title,length)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2017-09-13
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      相关资源
      最近更新 更多