qiyeboy / SpiderBook

<<python爬虫开发与项目实战>>书籍配套源码和说明
MIT License
984 stars 520 forks source link

第十四章[知乎爬虫]--cookie序列化的疑问 #120

Open yjshi2015 opened 5 years ago

yjshi2015 commented 5 years ago

cookie序列化的代码并没有放在'方法内部',而是跟方法同级的.它是如何被触发执行的?

   # 序列化逻辑
   if not os.path.exists('session.txt'):
       with open('session.txt','wb') as f:
           import pickle
           cookies = response.request.headers['cookie']
           cookieDict={}
           for cookie in cookies.split(';'):
               key,value = cookie[0:cookie.find('=')], cookie[cookie.find('=')+1:]
               cookieDict[key]=value
           pickle.dump(cookieDict,f)

    # 方法
    def parse_user_info(self,response):
        ......