gpt4 book ai didi

html - rvest 可以使用 html_table 保留内联 html 标记,例如
吗?

转载 作者:搜寻专家 更新时间:2023-10-31 08:28:10 24 4
gpt4 key购买 nike

我正在尝试在 R 中抓取一个以 html 形式提供的表格。 Rvest 在从表格中获取所有文本方面非常有用,但我想保留在其 HTML 表单中出现的内联样式。

例如,表格中的文本可能是

"This is a sentence <BR> this is another sentence"

我想保留BR

我试过阅读整个表格:

my_table <- my_table_html %>% 
html_nodes("table") %>%
html_table(fill=TRUE)

我也试过在表格中选择特定的列:

my_column <- my_table_html %>% 
html_nodes(".Tabletitle:nth-child(2)") %>%
html_text()

任何想法将不胜感激

最佳答案

library(rvest)
library(xml2)

pg <- read_html("This is a sentence <BR> this is another sentence")

xml_find_all(pg, ".//br") %>% xml_add_sibling("p", "\n")

xml_find_all(pg, ".//br") %>% xml_remove()

html_text(pg)
## [1] "This is a sentence \n this is another sentence"

关于html - rvest 可以使用 html_table 保留内联 html 标记,例如 <br> 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30921626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com