2016-02-12 86 views
0

這是從每個職位(從forbes.com)獲得標題和日期在每一頁的帖子獲取數據。當我在foreach內打印日期時,標題和時間顯示正確的計數「30」。但我的問題是當我打印插入查詢,結果得到錯誤。每個頁面下面有15個帖子,代碼用於從兩個頁面獲取數據。請檢查下面的代碼,並幫助我解決這個問題?使用使用curl和PHP PHP的Dom

<?php 
require_once('dbconnect.php'); 

//use curl to get html content 
function getHTML($url) 
{ 
    $curl=curl_init(); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET"); 
    curl_setopt($curl, CURLOPT_COOKIEFILE, '/cookies.txt'); 
    $result = curl_exec($curl); 
    curl_close($curl); 
    return $result; 
} 

$url         = "http://www.forbes.com/search/post/REIT/15/All-time/0/"; 
$results        = getHTML($url); 
$dom_document      = new DOMDocument(); 
$dom_document->loadHTML($results); 
$remove[]       = ","; 
$remove[]       = " "; 
$remove[]       = "results"; 

/* ############################################################################ Total ###################################################################*/ 
$total_results      = @$dom_document->getElementsByTagName('div'); 
foreach ($total_results as $total_result) { 
    $total_result_class = $total_result->getAttribute('class'); 
    if(strstr($total_result_class, 'total_records')){ 
     $total_result_replace = str_replace($remove, '', $total_result->textContent); 
    } 
} 
$pages_divide = $total_result_replace/15;   //338.4666 
$pages_floor = floor($pages_divide);    //338 

for($i = 1; $i<= 2; $i++) { // $i<= 2 (two page) 
    $url_without_page  = "http://www.forbes.com/search/post/REIT/15/All-time/"; 
    $url_with_page   = $url_without_page . $i . '/'; 
    /*echo '<pre>'; 
     print_r($url_with_page);*/ 
    $url_pages    = getHTML($url_with_page); 
    $dom_document_pages  = new DOMDocument(); 
    $dom_document_pages->loadHTML($url_pages); 

    /* ############################################################################ Title ###################################################################*/ 
    $title_result        = array(); 
    $titles         = @$dom_document_pages->getElementsByTagName('h2'); 
    foreach ($titles as $title) { 
     foreach($title->childNodes as $nodes){ 
      if($nodes->tagName == 'a'){ 
       $title_result     = str_replace("'", "", $nodes->textContent); 
       /* echo '<pre>'; 
        print_r($title_result);*/ 
      } 
     } 

    } 

    /* ############################################################################# Time ###################################################################*/ 
    $time_result     = array(); 
    $times      = @$dom_document_pages->getElementsByTagName('time'); 
    foreach ($times as $time) { 
     $date      = new DateTime(); 
     $date_replace    = str_replace(",", "", $time->textContent); //Feb 10, 2016 
     $string_to_time   = strtotime($date_replace); 
     $date->setTimestamp($string_to_time); 
     $time_result    = $date->format('Y-m-d'); 
     /*echo '<pre>'; 
      print_r($time_result);*/ 
    } 
    $query_insert     = "INSERT INTO article_forbes(title, datetime) VALUE ('".$title_result."', '".$time_result."')"; 
    echo '<pre>'; 
    echo $query_insert; 
    //$mysqli->query($query_insert); 
} 
+0

你是什麼意思的時候正確顯示'「30」'?那不是時候。 – Barmar

+0

你爲什麼這樣做環獲得冠軍和時間,然後只將最後每個嗎?這有什麼設置'$ title_result'到一個數組,然後將其設置爲一個標題循環的地步。你用'$ time_result'做同樣的事情。 – Barmar

+0

@Barmar「30」表示計數 –

回答

0

你沒有在循環中插入數據。所以你只需插入最後一個標題和最後一次。

您將$title_result$time_result初始化爲數組,因此我懷疑您最初打算收集所有標題和時間。但是,不是將它們推到循環中的數組上,而是用字符串替換了數組。更改

$title_result = str_replace("'", "", $nodes->textContent); 

$title_result[] = $nodes->textContent; 

,這樣你推到陣列,並作出了類似的變化來$time_result

那麼你應該做的刀片在一個循環。

$stmt = mysqli_prepare("INSERT INTO article_forbes(title, datetime) VALUES (?, ?)"); 
mysqli_bind_param($stmt, "ss", $title, $time); 

for ($i = 0; $i < count($title_result); $i++) { 
    $title = $title_result[$i]; 
    $time = $time_result[$i]; 
    mysqli_execute($stmt); 
} 

使用一份聲明中這樣意味着你不需要從標題中移除特殊字符。

+0

目前我只能獲得15個條目但我已經爲30個條目編寫代碼。 –

+0

我沒有看到一個原因。它應該在每個頁面上獲得15個條目。 – Barmar

+0

對不起。我在另一個for循環中添加了for循環。現在一切都清晰而正確。感謝您的支持。 –