使用xpath获取节点值

时间:2014-03-30 20:31:30

标签: r xpath

我不确定如何使用xpath

在节点下获取值
<div class="home_delivery_promo_banner"><span class="fleft"> <strong>Next day delivery (now includes Saturdays)</strong></span>
到目前为止,我已经写了下面的代码。

 stock =   xpathSApply(page,'//div[@class="home_delivery_promo_banner"]//span[@class="fleft"]//strong//text()',xmlValue,"class") 

parenturl = "http://www.diy.com/nav/rooms/kitchens/handles-hinges/knob_handles/Cooke-and-Lewis-Dimpled-Knob-Pewter-Effect-9291589&bqrefcmp=AW&bqrefcre=AW_AFFILIATES"

srcpage = getURLContent(GET(parenturl)$url,timeout(10))
page = htmlParse(srcpage,useInternalNodes = T,encoding='UTF-8')    
stock =   xpathSApply(page,'//div[@class="home_delivery_promo_banner"]//span[@class="fleft"]//strong// text()',xmlValue,"class") 

非常感谢提前

1 个答案:

答案 0 :(得分:1)

这是正确的。确保您使用htmlParse而不是xmlParse。例如,下面的代码适用于我。

hp <- htmlParse('<div class="home_delivery_promo_banner"><span class="fleft"> <strong>Next day delivery (now includes Saturdays)</strong></span>')
xpathSApply(hp, '//div[@class="home_delivery_promo_banner"]//span[@class="fleft"]//strong//text()',xmlValue,"class")
# [1] "Next day delivery (now includes Saturdays)"