PHP 使用Xpath获取HTML节点内容

<?php

header("http-equiv: content-type;Content-type: text/html; charset=utf-8");
$url = 'http://www.hao6v.com/gvod/zx.html';
$outPageTxt = file_get_contents($url);
$dom = new DOMDocument();
@$dom->loadHTML($outPageTxt);
$dom->normalize();
$xpath = new DOMXPath($dom);
$data = $xpath->query('//*[@id="main"]/div[1]/div/ul/li/a');
//取值方式一
foreach ($data as $val) {
    $item = $val->childNodes;
    foreach ($item as $v) {
        $str = trim($v -> textContent);
        echo $str . '<br/>';
    }
}
//取值方式二
for ($i = 0; $i < $data->length; $i++) {
    $items = $data->item($i);
    $text = $items->nodeValue;
    echo $text . '<br/>';
}

 

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页