Xpath php fetch links

我的梦境 提交于 2019-12-02 04:10:52

问题


I'm using this example to fetch links from a website :

http://www.merchantos.com/makebeta/php/scraping-links-with-php/

$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
    $href = $hrefs->item($i);
    var_dump($href);
    $url = $href->getAttribute('href');
    echo "<br />Link stored: $url";
}

It works well; getting all the links; but I cannot get the actual 'title' of the link; for example if i have :

<a href="www.google.com">Google</a>

I want to be able to fetch 'Google' term too.

I'm little lost and quite new to xpath.


回答1:


Try this:

$link_title = $href->nodeValue;



回答2:


You are looking for the "nodeValue" of the Textnode inside the "a" node. You can get that value with

$title = $href->firstChild->nodeValue;

Full working example:

<?php
$dom = DomDocument::loadHTML("<html><body><a href='www.test.de'>DONE</a></body></html>");

$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    $title = $href->firstChild->nodeValue;
    echo "<br />Link stored: $url $title";
}

Prints:


Link stored: www.test.de DONE



来源:https://stackoverflow.com/questions/3291742/xpath-php-fetch-links

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!