php XPath爬取小说站内容

今天复习php的时候看到dom这个扩展,想了想在php里面貌似最大的用途就只是xpath

$content = file_get_contents("https://read.qidian.com/chapter/bLkfqG4_EUSoUTngny7bXQ2/lUKaD4PkCBRp4rPq4Fd4KQ2");
$doc = new DOMDocument('1.1', 'utf-8');
try {
    $doc->loadHTML($content);
} catch (Exception $e) {
    $e->getMessage();
}

$xpath = new DOMXPath($doc);
$elements = $xpath->query("//div[@class='read-content j_readContent']");
foreach ($elements as $element) {
    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
        echo $node->nodeValue . "\n";
    }
}

就到这了等过段时间工作稳定可能会详细的出一篇文章介绍php爬虫的

猜你喜欢

转载自blog.csdn.net/a99361481/article/details/81546467