我需要下载超过100.000张图片。图片有:.png,.jpg,.jpeg,.gif格式。我已获准使用这些图片。他们为我提供了一个包含所有网址的XML文件
网址具有结构
其他域/产品图片/代码/图像名称.jpg/.png/.gif
我有一个叫做 $codes[]
的 php 数组中的所有代码我也有数组上所有图像的完整路径$images[]
我需要下载所有这些图片并保持相同的结构
mydomain/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);
}
你能帮我让它工作吗,因为脚本根本不起作用
我可能会建议你更简单,更快捷地完成任务。将完整的 URL 写入列表.txt执行 wget -x -i list.txt 命令,该命令将下载所有图像并根据站点结构将它们放在适当的目录中。
响应
它工作得很好,它是否碰巧知道我是否可以设置 wget 将所有文件下载到某个位置,例如 HTTP 根文件夹?
wget 下载到它正在运行的文件夹中,因此您只需 cd 到该文件夹并在那里执行 wget。
此外,为了补充@Hlorofos答案,您可以使用 -nH,因此文件夹结构不包含主机 URL。