[英]How can i get the text from a child node with php DOMDocument
I've been writing a php code to get information from a site, so far i was able to get the href attribute, but i cant find a way to get the text from the child node "span", can someone help me?我一直在编写 php 代码来从站点获取信息,到目前为止我能够获取 href 属性,但是我找不到从子节点“span”获取文本的方法,有人可以帮助我吗?
html- > html->
<a class="js-publication" href="publication/247931167">
<span class="publication-title">An approach for textual authoring</span>
</a>
This is how i am currently able to get the href ->这就是我目前能够获得 href ->
@$dom->loadHTMLFile($curPage);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $element) {
$class_ = $element->getAttribute('class');
if (0 !== strpos($class_, 'js-publication')) {
$href = $element->getAttribute('href');
if(0 === stripos($href,'publication/')){
echo $href;//link para a publicação;
echo "\n";
}
}
}
You can use DOMXpath您可以使用DOMXpath
$html = <<< LOL
<a class="js-publication" href="publication/247931167">
<span class="publication-title">An approach for textual authoring</span>
</a>
LOL;
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->query("//a[@class='js-publication']") as $element){
echo $element->getAttribute('href');
echo $element->textContent;
}
//publication/247931167
//An approach for textual authoring
Or without the for
loop, if you just want one element :或者没有for
循环,如果你只想要一个元素:
echo $xpath->query("//a[@class='js-publication']/span")[0]->textContent;
echo $xpath->query("//a[@class='js-publication']")[0]->getAttribute('href');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.