我想从第一个笑话中获取所有<?php
$url = "http://sms.hindijokes.co";
$html = file_get_contents($url);
$doc = new DOMDocument;
$doc->strictErrorChecking = false;
$doc->recover = true;
@$doc->loadHTML("<html><body>".$html."
</body> </html>");
$xpath = new DOMXPath($doc);
$query1 = "//h2[@class='entry-title']/a";
$query2 = "//div[@class='entry-content']/p";
$entries1 = $xpath->query($query1);
$entries2 = $xpath->query($query2);
$var1 = $entries1->item(0)->textContent;
$var2 = $entries2->item(0)->textContent;
echo "$var1";
echo "<br>";
$f = 5;
for($i = 0; $i < $f; $i++){
echo $entries2->item($i)->textContent."\n";
}
?>
元素,所以基本上我制作了这个脚本:
<p>
这次我知道第一个笑话中有五个<p>
个元素,但是如果我希望它是自动脚本,那么有时候会有多于或少于五个Rails.application.routes.draw do
resources :articles do
resources :comments
end
root 'welcome#index'
元素,这会导致混乱。
答案 0 :(得分:0)
DOMXPath::query
返回DOMNodeList
个对象。使用DOMNodeList::length
属性。
$f = $entries2->length;
答案 1 :(得分:0)
尝试这种方式它返回null;但有些笑话有多个 p 标签,因此您最好通过自定义 class / id
找到它$i = 0;
while($entries2->item($i)->textContent!=NULL) {
echo "<br>";
echo $i." ".$entries2->item($i)->textContent;
$i++;
}
答案 2 :(得分:0)
您首先需要div
个p
元素,因此您的查询将是:
$entries2 = $xpath->query('//(div[@class='entry-content'])[1]/p');
现在,您可以使用p
循环(提取其html内容)迭代所有foreach()
个元素:
$innerHtml = '';
foreach ($entries2 as $entry) {
$children = $entry->childNodes;
foreach ($children as $child) {
$innerHtml .= $child->ownerDocument->saveXML($child);
}
}
$innerHtml = str_replace(["\r\n", "\r", "\n", "\t"], '', $innerHtml);