Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

r - Inputting NA where there are missing values when scraping with rvest

I want to use rvest to scrape a page which has titles and run times of talks at a recent conference and then combine the values into a 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)

If you look at the page, you will see that for one of the talks there is no value - and in View source there is no class="caption" for this talk

Is there any way I can substitute an NA to show missing values?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The simplest way is to select a node that encloses both of the nodes you want for each row, then iterate over them, pulling out both of the nodes you want at once. purrr::map_df is handy for not only iterating, but even combining the results into a nice tibble:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...