我想從第一個笑話都<p>
要素所以基本上我做了這個腳本:PHP從網站提取數據
<?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>
元素我希望它是自動化的腳本,有時會多於或少於五個元素,因此會造成混亂。
請參閱http://stackoverflow.com/questions/6366351/getting-dom-elements-by-classname –