1. 程式人生 > >php XPath爬取小說站內容

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爬蟲的