通过Atribute从TAG获取内容

发布于 2025-02-03 20:56:26 字数 431 浏览 4 评论 0原文

我需要从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伊面 2025-02-10 20:56:26

要检查是否有一个属性,您需要使用其名称来处理它

$thetag->hasAttribute('data-copy')

以获取数据拷贝的内容,您可以像

// <a data-copy="valueoftheattribute">
$thetag->getAttribute('data-copy') === 'valueoftheattribute'

To check if an attribute is there you need to address it with it's name

$thetag->hasAttribute('data-copy')

To get the content of data-copy you can compare it like

// <a data-copy="valueoftheattribute">
$thetag->getAttribute('data-copy') === 'valueoftheattribute'
我的奇迹 2025-02-10 20:56:26

您也可以使用XPath根据存在data-copy属性或其他更复杂的标准直接找到节点,而无需使用hasAttribute < /code>和getAttribute so:

$file='https://mypage.com/';

libxml_use_internal_errors( true );
$html=file_get_contents( $file );

$dom=new DOMDocument;
$dom->strictErrorChecking=false;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->loadHTML( $html );
libxml_clear_errors();

$xp=new DOMXPath( $dom );
$expr='//a[@data-copy]'; # find `a` nodes anywhere in the source document that simply have the data-copy attribute


# run the query
$col=$xp->query( $expr );

# iterate through any found nodes and display the content
if( $col && $col->length > 0 ){
    foreach( $col as $i => $node )printf('<div>[%d] - %s</div>', $i, $node->nodeValue );
}

You can also use an XPath to find the nodes directly based upon the existence of the data-copy attribute or other, more complicated criteria without needing to use hasAttribute and getAttribute like so:

$file='https://mypage.com/';

libxml_use_internal_errors( true );
$html=file_get_contents( $file );

$dom=new DOMDocument;
$dom->strictErrorChecking=false;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->loadHTML( $html );
libxml_clear_errors();

$xp=new DOMXPath( $dom );
$expr='//a[@data-copy]'; # find `a` nodes anywhere in the source document that simply have the data-copy attribute


# run the query
$col=$xp->query( $expr );

# iterate through any found nodes and display the content
if( $col && $col->length > 0 ){
    foreach( $col as $i => $node )printf('<div>[%d] - %s</div>', $i, $node->nodeValue );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文