2016-11-29 43 views
1

我無法刮取產品圖片。我正在使用ajax。我的AJAX文件test.html的,這裏是我的代碼: -刮臉產品圖片網址,來自內容上傳的網站dynamiclly

$("#click_me").click(function() { 
    $.ajax({ 
     url: "test.php", 
     asyn:false, 
     success: function(result){ 
     console.log(result); 
    }}); 
}); 

test.php的文件代碼: -

$url="http://www.kohls.com/catalog/bedroom-mattresses-accessories-furniture.jsp?CN=Room:Bedroom+Category:Mattresses%20%26%20Accessories+Department:Furniture&cc=bed_bath-TN3.0-S-mattresses"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 "); 
$out = curl_exec($ch); 
curl_close($ch); 
$out = str_replace("\n", '', $out); 
echo $out; 

注:請檢查$url。圖像動態填充,我們無法刮取它們。請我需要快速指導,我已經使用pythonjs以及刮他們,但沒有工作! 謝謝!

回答

0

您需要解析出HTML中的圖像。 DOMDocument是一個很好的選擇。

示例代碼(未經測試,但在理論上應該工作)

$url="http://www.kohls.com/catalog/bedroom-mattresses-accessories-furniture.jsp?CN=Room:Bedroom+Category:Mattresses%20%26%20Accessories+Department:Furniture&cc=bed_bath-TN3.0-S-mattresses"; 

$html=file_get_contents($url); 
[email protected]::loadHTML($html); 
foreach($domd->getElementsByTagName("img") as $img){ 
$src=$img->getAttribute("src"); 
if(empty($src)){continue;} 
$src='http://www.kohls.com'.$src; 
$filename=basename($src); 
echo "downloading ".$filename.PHP_EOL; 
file_put_contents($filename,file_get_contents($src)); 
} 

只是你的捲曲功能代替的file_get_contents如果你想捲曲 (也這是相當內存餓了,因爲整個圖像將被下載到RAM不管它有多大,用curl,你可以用CURLOPT_FILE優化它直接寫入文件,如果你想從NASA下載圖像,可以節省很多RAM)

+0

沒有東西是$ html =的file_get_contents($網址);將不會獲取產品圖片的html。因爲「產品圖片」使用Ajax調用進行上傳,並且當我發送請求來刮取網址時,它不會刮擦產品html。所以根據您的邏輯,我只會陷入循環,我可能永遠不會獲取圖像的src。 –