"""访问网址,并且将网址的内容返回给文件,如果有文件且不为0。则读取即可。若没有则新建文件""" from urllib.request import urlopen def wrapper(func): def inner(*args,**kwargs): """装饰函数之前执行的操作""" import os if os.path.getsize('web'): with open('web','rb') as f: return f.read() ret = func(*args, **kwargs) with open('web','wb') as f: f.write(b'*******'+ret) return ret """装饰函数之后的执行的操作""" return inner @wrapper def get(url): content=urlopen(url).read() return content res=get('http://www.baidu.com') print(res)