我是XPath的新手,但我可以看到它有多强大.我正在查看此链接的源代码,只是想从以下两个页面中提取内容和用户名,为简单起见,它们位于源代码顶部附近.
content ="[存档] Simburgur的直播[离线]战争机器3"
<div class="username">Simburgur</div>
Run Code Online (Sandbox Code Playgroud)
这是我在R中的代码:
doc <- htmlParse("http://forums.epicgames.com/archive/index.php/t-672775.html")
xpathSApply(doc, "//head/meta[@name=\"description\"]")
Run Code Online (Sandbox Code Playgroud)
返回
[[1]]
<meta name="description" content="[Archive] Simburgur's Live Stream [Offline] Gears of War 3" />
Run Code Online (Sandbox Code Playgroud)
显然,在这个例子中,我想要的只是内容引用内的内容=但是卡住了,似乎无法让我的表达式返回我想要的字符串.
我重复.我是XPath的新手.:)
用途:
/*/head/meta[@name='description']/@content
Run Code Online (Sandbox Code Playgroud)
这仍然会选择一个属性节点,但是在PL中可能有一种简单的方法来获取属性的字符串值.
要获得字符串值,请使用:
string(/*/head/meta[@name='description']/@content)
Run Code Online (Sandbox Code Playgroud)
请注意:使用//
缩写可能会导致对XPath表达式的评估非常缓慢,因为它可能导致整个(子)树的线性遍历.
//
如果静态知道XML文档的结构,请始终避免使用.