def caiban_sina_weibo_login(user, pw): encoding = 'utf8' br = get_br() try: br.open('http://weibo.cn/') br.follow_link(text='登录'.encode(encoding)) except Exception as e: return '', str(e) form = list(br.forms())[0] user_control = form.controls[0] pw_control = form.controls[1] # remember_control = form.controls[2] user_control.value = user.encode(encoding) pw_control.value = pw.encode(encoding) #default is on #remember_control.value = ['on',] try: br.open(form.click()) except Exception as e: return '', str(e) url = br.geturl() gsid = get_url_query(url).get('gsid', '') # url = 'http://weibo.cn/?gsid=%(gsid)s&vt=4' % {'gsid': gsid} # br.open(url) content = br.response().read().decode(encoding, 'ignore') if content.find('请输入图片中的字符')!=-1: reason = 'yzm' elif content.find('您的微博帐号出现异常被暂时冻结')!=-1: reason = 'freeze' elif content.find('@我的')!=-1: reason = 'success' elif content.find('登录名或密码错误')!=-1: reason = 'auth fail' elif url.find('http://login.weibo.cn/login')!=-1: reason = 'fail' elif url.find('http://weibo.cn/pub/')!=-1: reason = 'redirect' else: reason = 'unknown' return gsid, reason
def br(self): if not self._br: self._br = get_br() return self._br