[英]PHP get html tag value with PHPDocumente()
这是简单的html代码,我想从中得到sorry
, promise
和back-link
值
<div id="core">
<h1 id="sorry">Sorry, something went wrong.</h1>
<p id="promise">
We're working on it and we'll get it fixed as soon as we can.
</p>
<p id="back-link">
<a id="back" href="https://www.facebook.com/">Go Back</a>
</p>
<div id="footer">
Facebook
<span id="copyright">
© 2018
</span>
<span id="help-link">
·
<a id="help" href="https://www.facebook.com/help/">Help Center</a>
</span>
</div>
</div>
用下面的代码我得到其中之一的结果,例如:
DOMElement Object
(
[tagName] => p
[schemaTypeInfo] =>
[nodeName] => p
[nodeValue] =>
Go Back
[nodeType] => 1
[parentNode] => (object value omitted)
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] => (object value omitted)
[nextSibling] => (object value omitted)
[attributes] => (object value omitted)
[ownerDocument] => (object value omitted)
[namespaceURI] =>
[prefix] =>
[localName] => p
[baseURI] =>
[textContent] =>
Go Back
)
但它是所有属性的唯一对象,而不是我想要的东西,我想从此html获取sorry
, promise
和back-link
值
我的代码:
<?php
$url = "http://localhost/error.html";
$html = file_get_contents($url);
$doc = new DOMDocument(); // create DOMDocument
libxml_use_internal_errors(true);
$doc->loadHTML($html); // load HTML you can add $html
//libxml_clear_errors();
if ($html) {
$divs = $doc->getElementById('core');
$xpath = new DOMXPath($doc);
$backLink = $xpath->query('//p[@id="back-link"]');
//$sorryId = $xpath->query('//h1[@id="sorry"]');
//$promiseId = $xpath->query('//p[@id="promise"]');
$node = $backLink->item(0);
$href = $node->getAttribute('href');
echo '<pre>';print_r($node);echo '</pre>';
}
?>
请帮助我修复此代码以从html获取值,在此先感谢
由于id
在html页面上必须唯一,因此可以使用简单的getElementById
:
if ($html) {
$sorry = $doc->getElementById('sorry');
var_dump($sorry->textContent);
$promise = $doc->getElementById('promise');
var_dump($promise->textContent);
// here I use `back` id as you need link
// from this element, not from `back-link`
$backLink = $doc->getElementById('back')->getAttribute('href');
var_dump($backLink);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.