【问题标题】:CSS code appears in html_nodes() output using rvestCSS 代码使用 rvest 出现在 html_nodes() 输出中
【发布时间】:2022-10-13 01:53:54
【问题描述】:

作为一个小爱好项目,我正在使用 rvest 从网站上抓取一些信息。但是,对于我尝试提取的一个特定节点,它似乎将 CSS 样式代码附加到开头。

URL <- 'https://www.thepioneerwoman.com/food-cooking/recipes/a41138141/apple-pie-cookies-recipe/'
recipe <- rvest::read_html(URL)
directions <- rvest::html_nodes(recipe, ".et3p2gv0") %>%
    rvest::html_text() %>%
    trimws()

这是输出中出现的内容:

[1] ".css-dt22uw{display:none;visibility:hidden;}Step .css-6ds1rq{border-right:thin solid #b20039;height:1rem;left:-3rem;position:absolute;top:0.45rem;width:1.4rem;}1.css-1baulvz{display:inline-block;}Melt the butter in a medium saucepan over medium-high heat. Add the apples and cook until they start to soften, 3 to 4 minutes. Stir in the brown sugar and lemon juice, bring to a simmer and cook until the apples are soft and the liquid is starting to reduce, 3 to 4 more minutes. Whisk the apple juice and cornstarch in a small bowl and add it to the pan. Cook, stirring, until the mixture thickens, about 1 more minute. Remove from the heat and let cool. "

我尝试了各种不同的节点,并使用了不同的 CSS 选择器,但无论如何,它仍然出现在输出中。

我可能最终只使用 gsub() 从字符串中删除它,但宁愿不这样做。

【问题讨论】:

    标签: html r rvest


    【解决方案1】:

    也许xml2::xml_remove() 可能会有所帮助。

    URL <- 'https://www.thepioneerwoman.com/food-cooking/recipes/a41138141/apple-pie-cookies-recipe/'
    recipe <- rvest::read_html(URL)
    directions <- rvest::html_nodes(recipe, ".et3p2gv0")
    
    toremove <- directions %>%
      rvest::html_node("style")
    
    xml2::xml_remove(toremove)
    
    directions %>%
      rvest::html_text(trim = T)
    

    【讨论】:

      【解决方案2】:

      XPath text() 有时非常方便,您可以将它与 css 选择器混合匹配或将选择器重写为 XPath:

      URL <- 'https://www.thepioneerwoman.com/food-cooking/recipes/a41138141/apple-pie-cookies-recipe/'
      recipe <- rvest::read_html(URL)
      
      # get a list of <li> elements with css selector and extract text from each elemnet with XPath
      directions_1 <- rvest::html_elements(recipe, "ol.et3p2gv0 li") %>%
        html_nodes(xpath="./text()") %>% 
        rvest::html_text() %>%
        trimws()
      
      # or use only XPath
      directions_2 <- rvest::html_elements(recipe, xpath='//ol[contains(@class, "et3p2gv0")]/li/text()') %>%
        rvest::html_text() %>%
        trimws()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-04
        • 1970-01-01
        • 2018-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-20
        相关资源
        最近更新 更多