2017-06-06 63 views
0

我正在編寫一個使用簡單的HTML DOM解析器進行網頁抓取的項目。我從我的數據庫中抓取網頁,提取內容並將其存儲在數據庫中。該代碼與第一個URL一起工作正常,但在剩餘的URL上它只是跳出循環。以下是我的代碼。爲什麼我的代碼跳出一個循環在php

include_once('Connections/elecom_connect.php'); 
include_once('dom/simple_html_dom.php'); 

mysqli_select_db($elecom_connect,$database_elecom_connect); 
$sql = "SELECT * FROM link_data"; 
$result_links = array(); 
$result_cates = ''; 
$result_subs = ''; 
$result_names = ''; 
$num = -1; 
$count = 0; 

$img = '.image-wrapper img'; 
$brand = 'h2.title span.brand'; 
$name = 'h2.title span.name'; 
$price = 'span.price-box'; 
$link = 'section.products a.link'; 

$site = new simple_html_dom(); 

$query = mysqli_query($elecom_connect,$sql); 

if (!$query){ 
    echo 'Database error: ' . mysqli_error($elecom_connect); 
} 

while ($row = mysqli_fetch_array($query)) { 
    $result_links[] = $row; 
} 

foreach($result_links as $link){ 
    $var = $link['link']; 
    if (!empty($var)) { 
     var_dump($var); 

     $site->load_file($var); 
     if (!empty($site)) { 
      $currentImg = $site->find($img); 
      $currentBrand = $site->find($brand); 
      $currentName = $site->find($name); 
      $currentPrice = $site->find($price); 
      $currentLink = $site->find($link); 

      $rid = $link['id']; 
      $rcates = $link['link_category']; 
      $rsubs = $link['link_subcategory']; 
      $rnames = $link['link_name']; 
      if (!empty($currentImg)) { 
       foreach($currentImg as $im){ 
        $count++; 

        if($count % 2 == 0 && $count < 40){ 
         $num++; 

         $cImg = $im->src; 
         $cBrand = "<p>".$currentBrand[$num]->plaintext."</p>"; 
         $cName = "<p>".$currentName[$num]->plaintext."</p>"; 
         $cPrice = "<p>".$currentPrice[$num]->plaintext."</p>"; 
         //$cLink = $currentLink[$num]->href; 

         $content = file_get_contents($cImg); 
         //Store in the filesystem. 
         $save_path = "cachedPages/$rid.$num.jpg"; 
         file_put_contents($save_path,$content); 

         $insertSQL = "INSERT INTO item_detail (item_name, item_brand, item_price, item_img, item_cate, item_sub_cate,filter_by) VALUES ('$cName', '$cBrand', '$cPrice','$save_path','$rcates','$rsubs','$rnames')"; 

         mysqli_select_db($elecom_connect,$database_elecom_connect); 
         $Result1 = mysqli_query($elecom_connect,$insertSQL) or die(mysqli_error(   $elecom_connect)); 

         echo 'Success'; 


        } 
       } 
      } 

     } 
    } 
    $site->clear(); 
} 

這是我得到的錯誤代碼。

Fatal error: Uncaught Error: Call to a member function find() on null in dom/simple_html_dom.php:1113 Stack trace: #0

我該怎麼辦?

+1

確保您的$ image $ brand $ price $ link和$ name在dom/simple_html_dom.php文件中未設置爲空 – AMH

+0

它們未設置爲null。它第一次運行第一個URL,但不能再運行第二個URL –

+0

我不能用空檢查重現它,但該錯誤是說'$ site'是'null'和'null'沒有一個'find()'方法。它是如何越過空的IDK。這是'simple_html_dom'文件嗎? – nerdlyist

回答

0

這行代碼是不正確的:

$site = new simple_html_dom(); 

你顯然並不需要基於在GitHub上的例子目錄要做到這一點https://github.com/samacs/simple_html_dom/tree/master/example

你想要做的是使用一個兩種方法

file_get_htmlstr_get_html當您包含include_once('dom/simple_html_dom.php');時會加載它們。

所以你真的想看到

$site = file_get_html($url); //URL to a site you are parsing ie 'http://www.google.com/' 
//OR 
$site = str_get_html($str); // String file to some html file 

這在您閱讀的代碼實際上創建了一個$dom_node上它具有的find方法。

你有什麼奇怪的原因是因爲你正在創建和對象,當你檢查if(!empty($site))它返回true,因爲有一個對象。但是,內部dom_node設置不正確。

當你進入這個行林達1113文件不是你的你有一個空dom_nodenull->find()將拋出你所得到的錯誤。

+0

嗯,我會試試....感謝您的回答 –

+0

請一旦我拿到我的筆記本電腦並嘗試給出的答案,我就會upvote。 –

+0

這是我使用ur建議後的新錯誤信息 警告:file_get_contents():stream不支持在第75行的dom/simple_html_dom.php中尋找012, 警告:file_get_contents():無法尋求位置-1在第75行dom/simple_html_dom.php中的流# –

-2

您將每個行都替換爲整個數組,以便只刪除最後一個網址。

$result_links = array(); 
while ($row = mysqli_fetch_array($query)) 
{ 
    array_push($result_links, $row); 
} 
+1

'[]'表明它是一個數組,所以你說的是不正確的。 – RST

+0

@RST我知道它是一個數組,但代碼不會追加到數組,它會替換數組。 –

+0

問題是執行了第一個URL,但其餘的都沒有... –

相關問題