Equivalent of which in scraping?

后端 未结 2 1904
清歌不尽
清歌不尽 2020-12-12 02:36

I\'m trying to run some scraping where the action I take on a node is conditional on the contents of the node.

This should be a minimal example:

XML          


        
2条回答
  •  感情败类
    2020-12-12 03:19

    An alternate approach:

    library(tidyverse)
    library(rvest)
    
    XML <- '
    
        Really L...
    
    Short
    '
    
    pg <- read_html(XML)
    
    html_nodes(pg, "td[class='id-tag']") %>%
      map_chr(function(x) {
        if (xml_find_first(x, "boolean(.//span)")) {
          x <- html_nodes(x, xpath=".//span/@title")
        }
        html_text(x)
      })
    
    ## [1] "Really Long Text" "Short"
    

提交回复
热议问题