在 rvest 中将文本保留在 HTML 内

Keep text inside HTML in rvest

我想知道如何在特定属性和网站 URL 上 运行 之后只保留 <> 之间的文本。这是我在输出中得到的字符集

{xml_nodeset (11)}
 [1] <td id="open">1.1041</td>
 [2] <td id="open">1.1043</td>
 [3] <td id="open">1.1049</td>
 [4] <td id="open">1.1043</td>
 [5] <td class="right" id="open">47.617</td>
 [6] <td class="left" id="open">MA</td>

理想情况下,我想隔离包含的文本并得到这个

[1] 1.1041
[2] 1.1043
[3] 1.1049
[4] 1.1043
[5] 47.617
[6] MA

但到目前为止,通过使用 html_text 函数,我得到了一个在值之间带有“”的连接字符串,这不是我想要的

[1] "1.1041" "1.1043" "1.1049" "1.1043" "47.617" "MA"  

由于最后一个值 MA,所有内容都被强制转换为字符串格式。这就是为什么您会在数字周围加引号。

您可以将所有内容转换为数字,但最后一个值将被强制转换为 NA

q <- c("1.1041", "1.1043", "1.1049", "1.1043", "47.617", "MA")
as.numeric(q)

# The output of the previous command is:
[1]  1.1041  1.1043  1.1049  1.1043 47.6170      NA
Warning message:
NAs introduced by coercion 

所以您必须决定您希望数据采用哪种格式。