使用DOMDocument获取标题标记值

时间:2014-10-06 21:06:49

标签: php html scrape

我想获取我网站所有网页的<title>标记的值。我试图只在我的网站域上运行脚本,并获取我网站上的所有页面链接,以及它们的标题。

这是我的代码:

$html = file_get_contents('http://xxxxxxxxx.com');
//Create a new DOM document
$dom = new DOMDocument;

//Parse the HTML. The @ is used to suppress any parsing errors
//that will be thrown if the $html string isn't valid XHTML.
@$dom->loadHTML($html);

//Get all links. You could also use any other tag name here,
//like 'img' or 'table', to extract other tags.
$links = $dom->getElementsByTagName('a');

//Iterate over the extracted links and display their URLs
foreach ($links as $link){
    //Extract and show the "href" attribute.
         echo $link->nodeValue;
    echo $link->getAttribute('href'), '<br>';
}

我得到的是:<a href="z1.html">z2</a>我得到z1.htmlz2 .... 我的z1.html有一个名为title的{​​{1}}。我想获得z3z1.html,而不是z3。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

向hitesh的答案添加一点,以检查元素是否具有属性并且所需属性是否存在。如果获得“标题”也是如此。在尝试抓取第一个元素之前,元素确实会返回至少一个项目($ a_html_title-&gt; item(0))。

并为curl添加了一个关注位置的选项(我需要将其用于google.com的硬编码测试)

foreach ($links as $link) {

    //Extract and show the "href" attribute.
    if ($link->hasAttributes()){
        if ($link->hasAttribute('href')){

            $href = $link->getAttribute('href');
            $href = 'http://google.com';   // hardcoding just for testing

            echo $link->nodeValue;
            echo "<br/>".'MY ANCHOR LINK : - ' . $href . "---TITLE--->";
            $a_html = my_curl_function($href);

            $a_doc = new DOMDocument();
            @$a_doc->loadHTML($a_html);
            $a_html_title = $a_doc->getElementsByTagName('title');

            //get and display what you need:
            if ($a_html_title->length){
                $a_html_title = $a_html_title->item(0)->nodeValue;
                echo $a_html_title;
                echo '<br/>';
            }

        }
    }

}

function my_curl_function($url) {
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERAGENT, 'name');
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, TRUE);    // added this
    $html = curl_exec($curl_handle);
    curl_close($curl_handle);
    return $html;
}

答案 1 :(得分:0)

您需要制作自己的自定义函数并在适当的位置调用它,如果您需要从锚标记的页面中获取多个标记,则只需创建新的自定义函数。

下面的代码将帮助您入门

$html = my_curl_function('http://www.anchorartspace.org/');
$doc = new DOMDocument();
@$doc->loadHTML($html);
$mytag = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $mytag->item(0)->nodeValue;

$links = $doc->getElementsByTagName('a');

//Iterate over the extracted links and display their URLs
foreach ($links as $link) {

//Extract and show the "href" attribute.
    echo $link->nodeValue;
    echo "<br/>".'MY ANCHOR LINK : - ' . $link->getAttribute('href') . "---TITLE--->";

    $a_html = my_curl_function($link->getAttribute('href'));
    $a_doc = new DOMDocument();
    @$a_doc->loadHTML($a_html);
    $a_html_title = $a_doc->getElementsByTagName('title');
//get and display what you need:
    $a_html_title = $a_html_title->item(0)->nodeValue;
    echo $a_html_title;
    echo '<br/>';
}
echo "Title: $title" . '<br/><br/>';

function my_curl_function($url) {
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERAGENT, 'name');
    $html = curl_exec($curl_handle);
    curl_close($curl_handle);
    return $html;
}

如果您需要更多帮助,请告诉我