PHP CURL / XPATH - 链接无效
PHP CURL / XPATH - Links not working
我正在使用以下代码为 http://psnc.org.uk/our-latest-news-category/psnc-news/
抓取一些外部 div
我想抓取 PSNC 新闻最新消息部分
$ch = curl_init("http://psnc.org.uk/our-latest-news-category/psnc-news/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$document = new DOMDocument;
libxml_use_internal_errors(true);
$document->loadHTML($output);
$xpath = new DOMXPath($document);
$tweets = $xpath->query("//article[@class='news-template-box']");
echo "<html><body>";
foreach ($tweets as $tweet) {
echo "\n<p>".$tweet->nodeValue."</article>\n";
}
echo "</html></body>";
它成功地抓取了文本,但实际上所有元素的链接/href's/图像都没有出现。
我是不是漏掉了什么?
DOMNode::nodeValue == DOMNode::textContent,只打印文本内容。
http://php.net/manual/en/class.domnode.php#domnode.props.nodevalue
$tweets = $xpath->query("//article[@class='news-template-box']");
foreach ($tweets as $tweet) {
echo $document->saveHTML($tweet);
}
我正在使用以下代码为 http://psnc.org.uk/our-latest-news-category/psnc-news/
抓取一些外部 div我想抓取 PSNC 新闻最新消息部分
$ch = curl_init("http://psnc.org.uk/our-latest-news-category/psnc-news/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$document = new DOMDocument;
libxml_use_internal_errors(true);
$document->loadHTML($output);
$xpath = new DOMXPath($document);
$tweets = $xpath->query("//article[@class='news-template-box']");
echo "<html><body>";
foreach ($tweets as $tweet) {
echo "\n<p>".$tweet->nodeValue."</article>\n";
}
echo "</html></body>";
它成功地抓取了文本,但实际上所有元素的链接/href's/图像都没有出现。
我是不是漏掉了什么?
DOMNode::nodeValue == DOMNode::textContent,只打印文本内容。
http://php.net/manual/en/class.domnode.php#domnode.props.nodevalue
$tweets = $xpath->query("//article[@class='news-template-box']");
foreach ($tweets as $tweet) {
echo $document->saveHTML($tweet);
}