python abnormal phenomena recorded

 

1, the dictionary is the only key, but can not read the following

 

 1 from urllib.request import urlopen
 2 import os
 3 
 4 url_dic = {}
 5 
 6 def cache(func):
 7     def inner(*args,**kwargs):
 8         if os.path.getsize(url_dic[url_in]):
 9             with open(url_dic[url_in],'rb') as f:
10                 ret = f.read()
11             return ret
12         ret = func(*args,**kwargs)
13         with open(url_dic[url_in],'wb') as f:
14             f.write(b'****'+ret)
15         return ret
16     return inner
17 
18 @cache
19 def get_url(url):
20     code = urlopen(url).read()
21     return code
22 
23 while 1:
24     url_in = 'http://'+input('输入网址:')
25     with open('cache_'+str(len(url_dic)),'wb') as f0:
26         f0.write(b'')
27     url_dic.setdefault(url_in,'cache_'+str(len(url_dic)))
28     print(url_dic)
29 
30     print(url_dic)
31     ret = get_url(url_in)
32     print(ret)
Simple to obtain and cache the page

 

Guess you like

Origin www.cnblogs.com/xiuyou/p/11277141.html