2016-11-29 45 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。 –