简体   繁体   中英

HTML Parsing URL with DOMDocument and DOMXPath - Get element by ID

I've started to develop an script so I can parse an HTML DOM elements.

Here is what I have done already:

<?PHP
// to retrieve selected html data, try these DomXPath examples:

$url = 'http://www.sportsdirect.com/nike-satire-mens-skate-shoes-242188?colcode=24218822';
libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$elements = $xpath->query("*/span[@id='ProductName']");

if (!is_null($elements)) {
  foreach ($elements as $element) {
    echo "<br/>[". $element->nodeName. "]";

    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "\n";
    }
  }
}
?>

     

All what I want is to get the text contained in the HTML element <span id="ProductName"></span>

The problem with my script is that I get Blank screen only, no results at all.

Can you please help me out understand how this thing works and make it.

Thanks in advance!

AYou should check whether your query yielded any elements (DOMNodelist). Check it first then get the element.

$elements = $xpath->query('//span[@id="ProductName"]');
if($elements->length > 0) {
    echo $elements->item(0)->nodeValue;
}

Sidenote: cant test this though im on mobile but this should be the basic idea

Here you have example:

http://php.net/manual/en/class.domxpath.php

You can do that:

$val=$xpath[0]->nodeValue;
var_dump($val);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM