python 下载图片完整版本

import os,sys,time,json,time
import socket,random,hashlib
import requests,configparser
import json,re
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool


def getpicurl(url):
    print(url + "\r\n")
    html = requests.get(url).text
    pic_url = re.findall('img src="(.*?)"',html,re.S)
    for key in pic_url:
        #print(os.path.basename(key) + "\r\n")
        print(key + "\r\n")
        down_pic(key, "images/" + os.path.basename(key))
    #print(pic_url)


def down_pic(picpath, lockpic):
    ret = False;
    #print(picpath + "\r\n" + lockpic)
    r = requests.get(picpath)
    if(r.status_code==200):
         open(lockpic, 'wb').write(r.content)
         ret = True
    return ret

def main():
    path = os.getcwd() + '/images/';                #创建一个目录存放结果
    if(False==os.path.isdir(path)):
        os.mkdir(path)

    print("开始")
    print("*" * 60)
    for i in range(0,355):
        url = "http://www.mzitu**-.com/zipai/comment-page-" + str(i)
        getpicurl(url)
    print("*" * 60)
    print("结束")

if __name__ == '__main__':
    main()

 

你可能感兴趣的:(python 下载图片完整版本)