Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath within R using XML package

Tags:

r

xpath

I am new to XPath, but I can see how powerful it is. I am looking at the source code of this link and simply want to extract the contents and username from the following two pieces of the page, which for simplicity sake are located near the top of the source code.

content="[Archive] Simburgur's Live Stream [Offline] Gears of War 3"

<div class="username">Simburgur</div>

Here is my code within R:

doc <- htmlParse("http://forums.epicgames.com/archive/index.php/t-672775.html")
xpathSApply(doc, "//head/meta[@name=\"description\"]")

which returns

[[1]]
<meta name="description" content="[Archive]  Simburgur's Live Stream [Offline] Gears of War 3" /> 

Obviously, in this example, all I want is what is inside the quotes of content= but am stuck and can not seem to get my expression to return the string I want.

I repeat. I am new to XPath. :)

like image 557
Btibert3 Avatar asked Feb 02 '11 02:02

Btibert3


2 Answers

Use:

/*/head/meta[@name='description']/@content

This still selects an attribute node, but probably there is an easy way in your PL to get the string value of the attribute.

To get just the string value, use:

string(/*/head/meta[@name='description']/@content)

Do note: Using the // abbreviation may result in very slow evaluation of the XPath expression, because it may cause a linear traversal of a whole (sub)tree.

Always avoid using // if the structure of the XML document is statically known .

like image 148
Dimitre Novatchev Avatar answered Nov 15 '22 22:11

Dimitre Novatchev


You're close. This should do it.

//head/meta[@name=\"description\"]/@content

The brackets are constraining the choice of meta tags, but you still have to specify the attribute you want.

like image 28
Mark Thomas Avatar answered Nov 15 '22 20:11

Mark Thomas