使用 bash 和 wget 下载具有不同扩展名的文件
Download files with differents extensions with bash and wget
我正在尝试下载带有 bash
和 wget
不同扩展名的图像。我必须直接定位文件,因为我在父文件夹上有 403 错误,所以我不能通过定位父文件夹来使用 -A
选项。
现在我有以下代码,它适用于 .jgp
但是如果下一个文件是 .png
例如我的循环中断并继续到下一个文件夹所以 .png
当前文件夹中没有下载。
for i in {1..945}; do
for j in {01..100}; do
j=`printf '%02d' $j`
ret=$(wget -O ch${i}-${j}.jpg https://www.domain.co/uploads/chapters/chapitre-${i}/${j}.jpg 2>&1)
if [[ "$ret" =~ 404\ Not\ Found ]]; then
break
fi
done
done
无论扩展名如何,我都找不到下载文件的方法。
编辑:
所以这是我给感兴趣的人的最后一个片段。
它的作用是:
递归爬网文件夹及其内容
检查文件是 .jpg
还是 .png
然后相应地重命名它
返回上一个循环(父文件夹)如果 404
删除使用 404 创建的空文件
for i in {245..945}; do
for j in {01..60}; do
j=`printf '%02d' $j`
ret=$(wget -O ch${i}-${j}.jpg https://www.yourdomain.co/uploads/parentfoldernumber-${i}/image-${j}.jpg 2>&1)
if [[ $? -ne 0 ]]; then
ret=$(wget -O ch${i}-${j}.png https://www.yourdomain.co/uploads/parentfoldernumber-${i}/image-${j}.png 2>&1)
fi
if [[ "$ret" =~ 404\ Not\ Found ]]; then
break
fi
done
find . -type f -empty -delete
done
我尝试了一些东西,也许这会对你有所帮助,
$?
是 bash 的命令,returns 是最后一个命令 运行.
的输出
如果没有找到*.png,则输出不会为0。
你可以这么说,
if [[ $? -ne 0 ]]; then
ret=$(wget -O ch${i}-${j}.png http://localhost/l4urstyle/wp-content/uploads/2018/${i}/${j}.png 2>&1)
fi
if [[ "$ret" =~ 404\ Not\ Found ]]; then
echo "breaking code"
break
fi
希望对您有所帮助。
如果这有效。请接受答案。 :D
关于正在下载的 0 个八位字节大小的文件。我会想出一些办法,然后让您知道我可以如何帮助您。
快乐编码:D
我正在尝试下载带有 bash
和 wget
不同扩展名的图像。我必须直接定位文件,因为我在父文件夹上有 403 错误,所以我不能通过定位父文件夹来使用 -A
选项。
现在我有以下代码,它适用于 .jgp
但是如果下一个文件是 .png
例如我的循环中断并继续到下一个文件夹所以 .png
当前文件夹中没有下载。
for i in {1..945}; do
for j in {01..100}; do
j=`printf '%02d' $j`
ret=$(wget -O ch${i}-${j}.jpg https://www.domain.co/uploads/chapters/chapitre-${i}/${j}.jpg 2>&1)
if [[ "$ret" =~ 404\ Not\ Found ]]; then
break
fi
done
done
无论扩展名如何,我都找不到下载文件的方法。
编辑:
所以这是我给感兴趣的人的最后一个片段。
它的作用是:
.jpg
还是 .png
然后相应地重命名它for i in {245..945}; do
for j in {01..60}; do
j=`printf '%02d' $j`
ret=$(wget -O ch${i}-${j}.jpg https://www.yourdomain.co/uploads/parentfoldernumber-${i}/image-${j}.jpg 2>&1)
if [[ $? -ne 0 ]]; then
ret=$(wget -O ch${i}-${j}.png https://www.yourdomain.co/uploads/parentfoldernumber-${i}/image-${j}.png 2>&1)
fi
if [[ "$ret" =~ 404\ Not\ Found ]]; then
break
fi
done
find . -type f -empty -delete
done
我尝试了一些东西,也许这会对你有所帮助,
$?
是 bash 的命令,returns 是最后一个命令 运行.
如果没有找到*.png,则输出不会为0。 你可以这么说,
if [[ $? -ne 0 ]]; then
ret=$(wget -O ch${i}-${j}.png http://localhost/l4urstyle/wp-content/uploads/2018/${i}/${j}.png 2>&1)
fi
if [[ "$ret" =~ 404\ Not\ Found ]]; then
echo "breaking code"
break
fi
希望对您有所帮助。 如果这有效。请接受答案。 :D 关于正在下载的 0 个八位字节大小的文件。我会想出一些办法,然后让您知道我可以如何帮助您。
快乐编码:D