将乌云漏洞图片下载到本地

#coding=utf-8
import MySQLdb
import re
import requests

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='123456',db='wooyunbugs',charset='utf8')
cursor = conn.cursor()

def q():
    sql = 'select * from `a`'
    cursor.execute(sql)
    for row in cursor.fetchall():
        id = int(row[0])
        print id,
        content1 = row[2]
        filenameList = f(content1)
        if len(filenameList)== 0:
            pass
        else:
            for imgUrl in filenameList:
                try:
                    downloadImageFile(imgUrl)
                    print 'success'
                except:
                    pass        

def f(c):
    re1 = re.compile('<img src="(http:\/\/static.wooyun.org\/wooyun\/.*?)"')
    s = re1.findall(c)
    return s


def downloadImageFile(imgUrl):  
    local_filename = imgUrl.split('/')[-1]
    local_filedir = imgUrl.split('/')[-2]  
    print "Download Image File=", local_filename  
    r = requests.get(imgUrl, stream=True, timeout=20)
    dirName =local_filedir
    import os
    if not os.path.exists(dirName):
        os.makedirs(dirName)
    with open(dirName+'/'+local_filename, 'wb') as f:  
        for chunk in r.iter_content(chunk_size=1024):  
            if chunk: 
                f.write(chunk)  
                f.flush()  
        f.close()
             
if __name__ == '__main__':
    q()

  

posted @ 2016-10-27 11:56  depycode  阅读(372)  评论(0编辑  收藏  举报