我想获得亚马逊客户评论中从 1 到 5 的评分。 我检查了来源,发现这部分看起来像
<div style="margin-bottom:0.5em;">
<span style="margin-right:5px;"><span class="swSprite s_star_5_0 " title="5.0 out of 5 stars" ><span>5.0 out of 5 stars</span></span> </span>
<span style="vertical-align:middle;"><b>Works great right out of the box with Surface Pro</b>, <nobr>October 5, 2013</nobr></span>
</div>
我想从 5 颗星中获得 5.0 颗星
<span>5.0 out of 5 stars</span></span> </span>
如何使用 xpathSApply 获取它?
谢谢!
最佳答案
我建议使用 selectr
包,它使用 css 选择器代替 xpath。
library(XML)
doc <- htmlParse('
<div style="margin-bottom:0.5em;">
<span style="margin-right:5px;">
<span class="swSprite s_star_5_0 " title="5.0 out of 5 stars" >
<span>5.0 out of 5 stars</span></span> </span>
<span style="vertical-align:middle;">
<b>Works great right out of the box with Surface Pro</b>,
<nobr>October 5, 2013</nobr></span>
</div>', asText = TRUE
)
library(selectr)
xmlValue(querySelector(doc, 'div > span > span > span'))
更新:如果您希望使用 xpath
,您可以使用 selectr
中的 css_to_xpath
函数找出合适的 xpath 命令,在这种情况下结果是
"descendant-or-self::div/span/span/span"
关于html - 如何从 <span> 和 </span> 之间的 html 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21963235/