class WeiboMsg: def __init__(self): self.wbdb = WeiboDB() # 获取用户信息前的初始化 def init_user(self, uid=None): self.weibomsg = { #'uid':'', #用户的id 'un': '', #用户用户名 'iu': '', #用户头像URL 'mid': '', #消息id 'mc': '', #消息内容 'nc': '', #消息中@的用户 'run': '', #转发的消息的用户名 'rmc': '', #转发的消息的内容 'pu': '', #消息中的图片 'rrc': '', #转发的消息的转发次数 'rcc': '', #转发的消息的评论次数 'rpage': '', #转发的消息的微博页面 'rpt': '', #转发的消息的发布时间 'rc': '0', #消息的转发次数 'cc': '0', #消息的评论次数 'srn': '', #消息来源 'page': '', #消息的微博页面 'pt': '' #消息的发布时间 } self.flag = 0 # 标记是否第一条记录,因为csv文件要在第一条记录前写标签 self.flag_un = 0 # 标记是否已经获得用户名 self.uid = uid # 处理一个分页的微博数据,需要继续处理返回True,停止处理返回False def get_content(self, content): self.content = content #self.weibomsg['uid'] = str(self.uid) #后加的用户id self.pos = 0 if self.flag_un == 0: self.get_weiboun() self.get_weiboiu() self.flag_un = 1 while self.pos >= 0: # 每次应该初始化一下 self.init_weibo() try: self.get_weibomid() except Exception, e: logging.exception('%s get_weibomid: ' + str(e), self.uid) continue if (self.wbdb.is_stop(self.weibomsg['mid'])): return False if self.pos == -1: break try: self.get_weibomc() except Exception, e: logging.exception('%s get_weibomc: ' + str(e), self.uid) continue try: self.get_retweet_info() except Exception, e: logging.exception('%s get_retweet_info: ' + str(e), self.uid) continue