提取节点信息

时间:2012-03-02 22:22:36

标签: r xml-parsing web-scraping

使用XML库,我已经解析了一个网页

basicInfo <- htmlParse(myURL, isURL = TRUE)

其相关部分是

<div class="col-left"><h1 class="tourney-name">Price Cutter Charity Championship Pres'd         by Dr Pep</h1><img class="tour-logo" alt="Nationwide Tour" src="http://a.espncdn.com/i/golf/leaderboard11/logo-nationwide-tour.png"/></div>

我可以设法提取锦标赛名称

tourney <- xpathSApply(basicInfo, "//*/div[@class='col-left']", xmlValue)

但也想知道使用alt标签的游览。在这种情况下,我想得到“Nationwide Tour”的结果

TIA和滚动所需的道歉

1 个答案:

答案 0 :(得分:3)

不知道R但是我对XPath非常好

试试这个:

tourney_name <- xpathSApply(basicInfo, "//*/div[@class='col-left']/h1/text()", xmlValue)
tourney_loc <- xpathSApply(basicInfo, "//*/div[@class='col-left']/img/@alt", xmlValue)

注意使用“@”提取属性和text()来提取文本节点(看起来R自动执行此操作),我修改后的tourney_name xpath应该做同样的事情,但是更清楚哪个部分被提取

相关问题