Reputation: 3253
i am trying to grab links from the Google search page. i am using the be below xpath to
//div[@id='ires']/ol[@id='rso']/li/h3/a/@href
grab the links. xPather evaluates it and gives the result. But when i use it with my php it doesn't show any result. Can someone please tell me what I am doing wrong? There is nothing wrong with the cURL.
below is my code
$dom = new DOMDocument();
@$dom->loadHTML($result);
$xpath=new DOMXPath($dom);
$elements = $xpath->evaluate("//div[@id='ires']/ol[@id='rso']/li/h3/a");
foreach ($elements as $element)
{
$link = $element->getElementsByTagName("href")->item(0)->nodeValue;
echo $link."<br>";
}
Sample Html provided by Robert Pitt
<li class="g w0">
<h3 class="r">
<a href="" class="l"><em>LINK</em></a>
</h3>
<button class="ws" title=""></button>
<div class="s">
META
</div>
</li>
Upvotes: 2
Views: 1867
Reputation: 5071
did you try
$element->getElementsByTagName("a")
instead of
$element->getElementsByTagName("href")
Upvotes: 0
Reputation: 51970
You can make life simpler by using the original XPath expression that you quoted:
//div[@id='ires']/ol[@id='rso']/li/h3/a/@href
Then, loop over the matching attributes like:
$hrefs = $xpath->evaluate(...);
foreach ($hrefs as $href) {
echo $href->value . "<br>";
}
Be sure to check whether any attributes were matched (var_dump($hrefs->length)
would suffice).
Upvotes: 3
Reputation: 57278
Theres no element called href, thats an attribute:
$link = $element->getElementsByTagName("href")->item(0)->nodeValue;
You can just use
$link = $element->getAttribute('href');
Upvotes: 0