【发布时间】: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() 从字符串中删除它,但宁愿不这样做。
【问题讨论】: