需要帮助保存一个url中存在的所有图像到一个文件夹
问题描述:
这不是一个重复的问题。请仔细阅读一次。我试图保存http://www.nobroker.in上存在的图像。特别是从:需要帮助保存一个url中存在的所有图像到一个文件夹
对于上述搜索查询有从1到paages想18.I从paage没有1的图像下载到18到folder.I写了下面的脚本:
<?php
function scrapeImages($base,$html)
{
$dom = new domDocument;
@$dom->loadHTML($html);
//find all the images in the HTML
$images = $dom->getElementsByTagName('img');
$imgArray = array();
//for each image tag, grab its src attribute and add it to the array
$i=0;
foreach ($images as $image) {
echo $base.$image->getAttribute('src').'<br>';
urltoimage($base.$image->getAttribute('src'));
$i++;
}
return $i;
}
function urltoimage($image_link)
{
//echo $image_link; die;
if (@getimagesize($image_link)) {
//$image_link ="https://www.dropbox.com/s/pt4wu5if3kwufr2/310890.jpg";
$no = mt_rand(10000000, 99999999);
$rand = $no."test".time();
$split_image = pathinfo($image_link);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , $image_link);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($ch);
curl_close($ch);
$updir= 'img';
$filename= $rand.".jpg";
$file_name = $updir.'/'.$filename;
$file = fopen($file_name , 'w') or die("X_x");
fwrite($file, $response);
fclose($file);
return $file_name;
} else {
return 'error';
}
}
$base = 'https://www.nobroker.in/property/ajax/rent/';
$url = 'https://www.nobroker.in/property/rent/bangalore/Koramangala?nbPlace=ChIJLfyY2E4UrjsRVq4AjI7zgRY&lat_lng=12.9279232,77.62710779999998&radius=1.0&sharedAccomodation=0&orderBy=nbRank,desc&radius=1&pageNo=1';
$images1 = scrapeImages($base,file_get_contents($url));
echo $images1 .' Images found';
但只获取2张图片,而不是100张图片。
答
我不知道你使用的PHP DOM库,但你目前正在寻找的HTML元素img
:
$images = $dom->getElementsByTagName('img');
然而,你的截图只有a
元素。图像包含在它们的属性中。你需要相应地解析。
+0
对不起,我需要与'img'标签的元素,我确实使用上述检索他们。但我无法检索它们。 我认为它是因为https。如果你有任何想法,请发表评论 –
执行中的任何错误? –
等待,您使用$ images = $ dom-> getElementsByTagName('img');但是在所提供的屏幕截图中我没有看到任何图像。这些是图像,但它们在锚标签内。你需要以不同的方式抓住这些。 –
没有错误 –