How to query a DOMNode using XPath in PHP? -
i'm trying bing search results xpath. here code:
$html = file_get_contents("http://www.bing.com/search?q=bacon&first=11"); $doc = new domdocument(); libxml_use_internal_errors(true); $doc->loadhtml($html); $x = new domxpath($doc); $output = array(); // grab urls foreach ($x->query("//li[@class='b_algo']") $node) { //$output[] = $node->getattribute("href"); $tmpdom = new domdocument(); $tmpdom->loadhtml($node); $tmpdp = new domxpath($tmpdom); echo $tmpdp->query("//div[@class='b_title']//h2//a//href"); } return $output;
this foreach iterates on results, want extract link , text $node
in foreach
, because $node
object can't create domdocument
it. how can query it?
first of all, xpath expression tries match non-existant href
subelements, query @href
attribute.
you don't need create new domdocument
s, pass $node
context item:
foreach ($x->query("//li[@class='b_algo']") $node) { var_dump( $x->query("./div[@class='b_title']//h2//a//@href", $node)->item(0) ); }
if you're interested in urls, query them directly:
foreach ($x->query("//li[@class='b_algo']/div[@class='b_title']/h2/a/@href") $node) { var_dump($node); }
Comments
Post a Comment