"""访问网址,并且将网址的内容返回给文件,如果有文件且不为0。则读取即可。若没有则新建文件"""from urllib.request import urlopendef 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@wrapperdef get(url): content=urlopen(url).read() return contentres=get('http://www.baidu.com')print(res)
转载于:https://www.cnblogs.com/sihong/p/11051847.html