Python3.5 图片下载代码

# coding:utf-8
import re
import os
import time
import random
import urllib
import urllib.request
import urllib.error

class MainProgram:
    # 定义类的属性
    def __init__(self, url=""):
        self.base_url = url  # 默认URL
        self.get_url = "https://mma.qq.com/Avengers/home/"
        self.goal_url =  'F:\phpStudy\WWW\\fulian\\'

    def imgDownload(self, imgUrl):

        full_url = self.get_url+imgUrl
        try:
            _path = self.goal_url
            file_path =_path +imgUrl
            fload_path = imgUrl[:imgUrl.rindex("/")+1]
            fload_path = fload_path.replace("/","\\")[:-1]
            print(fload_path)
            new_path = ""

            for i in fload_path.split("\\"):
                new_path = new_path+"\\"+i
                print(new_path)
                b = os.path.exists(_path + new_path)
                if b:
                    print("File Exist!")
                else:
                    #print("File no Exist!" + _path + new_path)
                    os.mkdir( _path + new_path)

            urllib.request.urlretrieve(full_url,file_path)
        except Exception as e:
            print(e)

        return

mainProgram = MainProgram()

mainProgram.get_url = "https://mma.qq.com/Avengers/home/"
mainProgram.goal_url = 'F:\phpStudy\WWW\\fulian\\'

url_arr =[
    'res/small/1/1.jpg',
    ]

for url in url_arr:
    print(url)
    mainProgram.imgDownload(url)

你可能感兴趣的:(小技巧,python3,爬虫)