我需要下载超过 100.000 张图片。图片有:.png、.jpg、.jpeg、.gif 格式。 我已获准使用这些图片。他们为我提供了一个包含所有 url 的 XML 文件
url有结构
otherdomain/productimages/code/imagename.jpg/.png/.gif
我将所有代码都放在一个名为 $codes[]
的 php 数组中
我还有数组 $images[]
我需要下载所有这些图片并保持相同的结构
我的域/productimages/code/imagename.jpg/.png/.gif
到目前为止,我通过互联网进行的研究是:
遍历所有页面(每个酒店代码)
$i = 1;
$r = 100000;
while ($i < $r) {
$html = get_data('http://otherdomain.com/productimages/'.$codes[$i].'/');
getImages($html);
$codes[$i++];
}
function getImages($html) {
$matches = array();
$regex = '~http://otherdomain.com/productimages/(.*?)\.jpg~i';
preg_match_all($regex, $html, $matches);
foreach ($matches[1] as $img) {
saveImg($img);
}
}
function saveImg($name) {
$url = 'http://otherdomain.com/productimages/'.$name.'.jpg';
$data = get_data($url);
file_put_contents('photos/'.$name.'.jpg', $data);
}
你能帮我让它工作吗,因为脚本根本不起作用
最佳答案
我可能会建议您使用更简单、更快速的方法来完成任务。 将完整的网址写入list.txt 执行 wget -x -i list.txt 命令,这将下载所有图像并根据站点结构将它们放在适当的目录中。
关于php - 从 html 下载图像并保持文件夹结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28944038/