简体   繁体   English

使用 DOMDocument 和 DOMXPath 的 HTML 解析 URL - 通过 ID 获取元素

[英]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.我已经开始开发一个脚本,以便我可以解析 HTML DOM 元素。

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>我想要的只是获取包含在 HTML 元素<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). A您应该检查您的查询是否产生了任何元素(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 http://php.net/manual/en/class.domxpath.php

You can do that:你可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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