rvest是否可以保留内嵌html标签,例如< br>使用html_table? [英] Can rvest keep inline html tags such as <br> using html_table?

查看:111
本文介绍了rvest是否可以保留内嵌html标签,例如< br>使用html_table?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中刮擦以html格式给出的表. Rvest在将所有文本从表格中取出时非常有用,但是我想保留以HTML形式出现的内联样式.

I am trying to scrape a table in R that I have been given in html form. Rvest was super useful in getting all of the text out of the table, but I would like to keep the inline styling that occurs in its HTML form.

例如,表中的文本可能是

For example, text in the table might be

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

我想保留BR

我尝试阅读整个表格:

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

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

I've also tried selecting specific columns in the table:

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

任何想法都将不胜感激

推荐答案

library(rvest)

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"

这篇关于rvest是否可以保留内嵌html标签,例如&lt; br&gt;使用html_table?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆