通过Atribute从TAG获取内容
我需要从atribute
中获取内容 a 命名data-copy
。
这是我到目前为止获得的非工作代码...
libxml_use_internal_errors(true);
$html=file_get_contents('https://mypage.com/');
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach(
$dom->getElementsByTagName('a') as $thetag){
if($thetag->getAttribute('a')=="data-copy"){echo "<h6>".$thetag->nodeValue."</h6>";}
}
I need to get the content from an atribute
inside a tag name a
named data-copy
.
This is the the non working code I got so far...
libxml_use_internal_errors(true);
$html=file_get_contents('https://mypage.com/');
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach(
$dom->getElementsByTagName('a') as $thetag){
if($thetag->getAttribute('a')=="data-copy"){echo "<h6>".$thetag->nodeValue."</h6>";}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要检查是否有一个属性,您需要使用其名称来处理它
以获取数据拷贝的内容,您可以像
To check if an attribute is there you need to address it with it's name
To get the content of data-copy you can compare it like
您也可以使用
XPath
根据存在data-copy
属性或其他更复杂的标准直接找到节点,而无需使用hasAttribute < /code>和
getAttribute
so:You can also use an
XPath
to find the nodes directly based upon the existence of thedata-copy
attribute or other, more complicated criteria without needing to usehasAttribute
andgetAttribute
like so: