Python 批量POST下载

今天无聊准备从imgloop.com上搜集一些图包,直接通过官方的下载档进行的.

原理比较简单,用urllib和httplib模拟发送POST,然后下载返回的数据.

直接在命令行运行:

python 文件名.py 参数1 参数2  

headers是发送头,如果不太明白可以抓包,用firefox里的firebug插件也行.

httplib.HTTPConnection所接收的参数是服务器(这里填写域名,如果要设置端口在后面加上:port)

request方法里设置发送方式(get或者post)和url地址

response.status中是http状态码,等于200时即为发送成功,在wiki上可以查到关于http状态码的信息

argv控制下载的文件名.

比如python download.py 100 200

就是下载100~200的文件,当然,你想要自己能成功使用需要修改里面的url格式和headers

#!/usr/bin/env python
import sys  
import httplib,urllib  
import threading

class download(threading.Thread):

    def __init__(self,number):
        threading.Thread.__init__(self,name=str(number))
        self.number=str(number)

    def run(self):
        params=urllib.urlencode({'name':'zipform'})
        headers={'Host':'imgloop.com',
                 'User-Agent':'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0',
                 'Accept':'*/*',
                 'Accept-Language':'zh-cn,zh;q=0.5',
                 'Accept-Encoding':'gzip, deflate',
                 'Accept-Charset':'GB2312,utf-8;q=0.7,*;q=0.7',
                 'Connection':'keep-alive',
                 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
                 'X-Requested-With':'XMLHttpRequest',
                 'Referer':'http://imgloop.com/Home/article/'+self.number,
                 'Content-Length':'12'
                 }
        conn=httplib.HTTPConnection('imgloop.com')
        conn.request('POST','http://imgloop.com/Home/zip/'+self.number,params,headers)
        response=conn.getresponse()
        if response.status==200:
            f=open(self.number+'.zip','w')
            f.write(response.read())
            f.close()
            print self.number+' Download Completed.'
        else:
            print self.number+' Download Failed.'
        conn.close()

if __name__ == '__main__':  
    for i in range(int(sys.argv[1]),int(sys.argv[2])):
        download(i).start()