【问题标题】:Placeholder NA for missing value with rvest - with XPath带有 rvest 的缺失值的占位符 NA - 带有 XPath
【发布时间】:2019-09-19 13:05:54
【问题描述】:

我看了这个问题:Inputting NA where there are missing values when scraping with rvest,答案很好!

目标:使用 xpath 实现相同的结果。

似乎在示例中使用了 css 标识符:

xx <- read_html("https://channel9.msdn.com/Events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference?sort=status&direction=desc&page=14")   
xx %>% html_nodes(xpath = "/html/body/main/section[2]/div/article") %>%  
       map_df(~list(title = html_nodes(.x, css = 'header h3 a') %>% 
        html_text() %>% {if(length(.) == 0) NA else .},    # replace length-0 elements with NA
        length = html_nodes(.x, css = 'a time') %>% 
        html_text() %>%  {if(length(.) == 0) NA else .}))

问题:用xpath怎么做?

xpath 应该是:

'/header/h3/a'

我尝试了什么:

## XPath
xx <- read_html("https://channel9.msdn.com/Events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference?sort=status&direction=desc&page=14")   
xx %>% html_nodes(xpath = "/html/body/main/section[2]/div/article") %>%  
  map_df(~list(title = html_nodes(.x, xpath = '/header/h3/a') %>% 
                 html_text() %>% {if(length(.) == 0) NA else .},    # replace length-0 elements with NA
               length = html_nodes(.x, xpath = '/a/time') %>% 
                 html_text() %>%  {if(length(.) == 0) NA else .}))

【问题讨论】:

    标签: r xpath rvest


    【解决方案1】:

    您的 xpath 应该是 header/h3/a,而不是 /header/h3/a。前导斜线表示您想再次从树的根部开始,而不是从当前节点开始。

    xx <- read_html("https://channel9.msdn.com/Events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference?sort=status&direction=desc&page=14")   
    xx %>% html_nodes(xpath = "/html/body/main/section[2]/div/article") %>%  
      map_df(~list(title = html_nodes(.x, xpath = 'header/h3/a') %>% 
                     html_text() %>% {if(length(.) == 0) NA else .},    # replace length-0 elements with NA
                   length = html_nodes(.x, xpath = 'a/time') %>% 
                     html_text() %>%  {if(length(.) == 0) NA else .}))
    
    #   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   
    

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 2017-03-01
      • 2016-07-17
      相关资源
      最近更新 更多