cookie_dict = {} for parent, dirnames, filenames in os.walk('./'): for filename in filenames: if filename.endswith('.weibo'): print filename with open(self.dir_temp + filename, 'r') as f: d = pickle.load(f)
if d.has_key('name') and d.has_key('value') and d.has_key('expiry'): expiry_date = int(d['expiry']) if expiry_date > (int)(time.time()): cookie_dict[d['name']] = d['value'] else: return {}
return cookie_dict
3,若缓存cookie过期,则再次从网络获取cookie
1 2 3 4 5 6 7
def get_cookie():
cookie_dict = get_cookie_from_cache() if not cookie_dict: cookie_dict = get_cookie_from_network() return cookie_dict
4,带cookie请求微博其他主页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
def get_weibo_list(self, user_id): import requests from bs4 import BeautifulSoup as bs cookdic = get_cookie()