我正在使用簡單的html dom刮取數據。然而,似乎有一些奇怪的行爲。即使寫入測試6次,它也只添加3條記錄。它是如何循環6次,只增加3行?循環6次,但只添加3條記錄
include('simple_html_dom.php');
$html = file_get_html("http://www.dailydot.com/tags/counter-strike/");
foreach($html->find("//li[@class='span4']") as $element) {
echo "test";
$title = strip_tags($element->find("//a[@class='article-title']/h3", 0));
$img = $element->find("//div[@class='picfx']/a/img[@class='lzy-ld']", 0)->getAttribute('data-original');
$link = $element->find("//a[@class='article-title']", 0)->href;
$date = $element->find("//p[@class='byline']/time", 0)->datetime;
mysqli_query($con, "INSERT INTO news (`title`, `url`, `image_url`, `news_text`, `referer_img`) VALUES ('$title', '$link', '$img', '$full_text_strip', 'test')");
}
你可能想要在循環之前確認var_dump($ html-> find(「// li [@ class ='span4']」))以確認它實際上是循環了6個元素。此外,你可能想檢查任何mysqli錯誤。此外,您的查詢並不安全。您需要正確地轉義您的變量。 – arothuis 2015-04-05 23:31:09
謝謝!將不勝感激,如果你作出回答:) – 2015-04-05 23:42:31