---
title: "批处理下载文件"
author: "iamsk"
date: "2009-07-01T22:31:06Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-07-01T22:31:06Z"
id: 418606
url: "https://iamsk.iteye.com/blog/418606"
---

以前写的，不过挺有用的，分享下，算是复习shell

用于下载http://pic.xhit.cn下的图片，属于硬编码，不过本网站的就不用改了

因为各个网站内部数据存储都各不相同，有的可能还加密，或重定向呢

所以即使是用python，也得根据不同网站进行分析

ps:附python下的html解析库，*SGMLParser、HtmlParser、BeautifulSoup（浓汤）*

我只用过前两个*，不过他们对标签的属性解析支持* 不是很好，第三个*这个据所说很好，对标签的属性解析支持很好用*

正题：

用法：./mget 父级目录 文件前缀 下载数量 保存目录

例：./mget upload1118 ai 10 ai

部分信息可以根据，右键图片属性，获得信息，比较笨，just for fun

```
#!/bin/bash
# author:sk
# filename:mget.sh
# $1 for first:document
document=$1

# $2 for second:the front of filename
front_name=$2

# $3 for the number of files

# $4 for the new document
new_document=$4

# count just is a Variable
count=0

filename=$count

# make a new document
mkdir $4

# downloading...
echo start downloading...
while [ $count != $3 ]
do
	count=`expr $count + 1`

	if [ $count -le 9 ]
		then

			#filename=$(($filename+1))
			filename=`expr $filename + 1`

			filename=00${filename}

	fi
	
	if [ $count -le 99 ] && [ $count -gt 9 ]
		then

			#filename=$(($filename+1))
			filename=`expr $filename + 1`


			filename=0${filename}

	fi 
	
	if [ $count -gt 99 ]
		then

			filename=`expr $filename + 1`

	fi
	
	echo "第 $count 个文件正在下载，文件名为 $filename ..."

	wget http://pic.xhit.cn/new/$document/$front_name$filename.jpg
done

# move the files to the new document
mv *.jpg $4
```