2017-03-18 147 views
0

我想從一個網站的數據存儲在一個內部div的所有圖像,我如何獲取所有圖像。我試過了,但它不能工作。這裏是我的代碼從網頁上刮取網頁數據

<?php 
$html = file_get_contents('http://en.vonvon.me/'); //get the html returned from the following url 

$pokemon_doc = new DOMDocument(); 

libxml_use_internal_errors(TRUE); //disable libxml errors 

if(!empty($html)){ //if any html is actually returned 

    $pokemon_doc->loadHTML($html); 
    libxml_clear_errors(); //remove errors for yucky html 

    $pokemon_xpath = new DOMXPath($pokemon_doc); 

    //get all the h2's with an id 
    $pokemon_row = $pokemon_xpath->query('div[class=desc ng-binding]'); 

    if($pokemon_row->length > 0){ 
     foreach($pokemon_row as $row){ 
      echo $row->nodeValue . "<br/>"; 
     } 
    } 
} 
?> 
+1

那麼,有什麼問題?沒有或錯誤的輸出?錯誤訊息?檢查錯誤日誌? Btw ..如果你的代碼不起作用,你應該刪除'@'符號,因爲這些符號會抑制錯誤消息,這在調試時很有用。 –

回答

0

你不能刪除JavaScript工作的網站[Angular],那麼在JavaScript執行後,抓取者沒有看到DOM。

但在另一方面,如果圖像是在DOM中,你可以使用

造成這種情況的最好的事情是Simple HTML DOM Parser

$html = file_get_html('http://vonvon.me/'); 

// Find all images 
foreach($html->find('img') as $element) 
     echo $element->src . '<br>'; 
+0

你能看到現在的代碼,圖像是在一個內部的股利,這就是爲什麼我這樣做,但它仍然無法正常工作。 – user7657378

+1

對不起,我沒有看到DOM,發佈答案之前,網站使用Angular JS,你不能廢棄它。 –