def post(self): #数据库校验 username = self.get_argument("un") password = self.get_argument("pw") code = self.get_argument("cd") print username, password, code sqlstr = """SELECT username FROM "user" WHERE username='******' and password='******';""" % ( username, utils.EncrypPassword(password)) rv = GxDB.Query(sqlstr) if rv['error'] != 0: self.write(BaseAPI.ReturnCode(rv['error'], rv['result'])) return if code.lower() != self.session.get('validatecode'): self.write(BaseAPI.ReturnCode(900, '验证码错误')) return #更新登录时间 sqlstr = """UPDATE "user" SET lastloginip='%s' WHERE username = '******'"""\ %(self.request.remote_ip, username) rv = GxDB.Exec(sqlstr) if rv is False: #self.write(BaseAPI.ReturnCode(900, '更新数据库错误')); return #成功了才写入session self.session.set('user', username) self.session.set( 'logintime', time.strftime('%Y-%m-%d(%H.%M.%S)', time.localtime(time.time()))) self.write(BaseAPI.ReturnCode(100))
def get_xsrf(self, url): index_url = url index_page = self.session.get(index_url, headers=BaseAPI.get_headers()) html = index_page.text pattern = r'name="_xsrf" value="(.*?)"' _xsrf = re.findall(pattern, html) print(_xsrf) return _xsrf[0]
def isLogin(self): # 通过查看用户个人信息来判断是否已经登录 url = "https://www.zhihu.com/settings/profile" login_page = self.session.get(url, headers=BaseAPI.get_headers(), allow_redirects=False) login_code = login_page.status_code if login_code == 200: return True else: return False
def login(self): if self.isLogin(): print('您已登录') else: account = input('请输入用户名\n') pwd = input('请输入密码\n') if re.match(r"^1\d{10}$", account): print('手机号登录') post_url = 'https://www.zhihu.com/login/phone_num' postdata = { '_xsrf': self.get_xsrf("https://www.zhihu.com"), "password": pwd, "remember_me": "true", "phone_num": account } else: if "@" in account: print('邮箱登录') else: print('输入账号有问题') return 0 post_url = 'https://www.zhihu.com/login/email' postdata = { '_xsrf': self.get_xsrf("https://www.zhihu.com"), "password": pwd, "remember_me": "true", "email": account } try: login_page = self.session.post(post_url, date=postdata, headers=BaseAPI.get_headers()) print(login_page.text) except: postdata["captcha"] = self.get_captcha() login_page = self.session.post(post_url, data=postdata, headers=BaseAPI.get_headers()) # login_code = eval(login_page.text) print(login_page) self.session.cookies.save()
def get_followees(self, user_address): followees_url = "https://www.zhihu.com" + user_address + "/followees" user_page = self.session.get(followees_url, headers=BaseAPI.get_headers(), allow_redirects=False) soup = BeautifulSoup(user_page.content, "lxml") follist = soup.select('div[class*="zm-profile-card"]') fol_user_addresses = [] for followees in follist: tag = followees.a["href"] fol_user_addresses.append(tag) return fol_user_addresses
def get_captcha(self): t = str(int(time.time() * 1000)) captcha_url = "https://www.zhihu.com/captcha.gif?r=" + t + "&type=login" r = self.session.get(captcha_url, headers=BaseAPI.get_headers()) with open("captcha.jpg", "wb") as f: f.write(r.content) f.close() try: im = Image.open("captcha.jpg") im.show() im.close() except: print(u'请到目录找到captcha.jpg 手动输入') captcha = input("please input the captcha\n") return captcha
def get_current_userInfo(self): url = "https://www.zhihu.com/settings/profile" login_page = self.session.get(url, headers=BaseAPI.get_headers(), allow_redirects=False) html = login_page.text print(login_page) pattern = r'<a href="(.*?)" class="zu-top-nav-userinfo ">' result = re.findall(pattern, html) soup = BeautifulSoup(html, 'lxml') title = soup.find('script', { 'data-name': 'ga_vars', 'class': 'json-inline' }) user_info = json.loads(title.contents[0]) user_info["user_address"] = result[0] return user_info
def get_user_deatailinfo(self, user_address): user_url = 'https://www.zhihu.com' + user_address userID = user_address[8:] user_page = self.session.get(user_url, headers=BaseAPI.get_headers(), allow_redirects=False) soup = BeautifulSoup(user_page.content, 'lxml') name = soup.find_all('span', {'class': 'name'})[1].string location = soup.find('span', {'class': 'location item'}) if location == None: location = 'None' else: location = location.string business = soup.find('span', {'class': 'business item'}) if business == None: business = 'None' else: business = business.string gender = soup.find('input', {'checked': 'checked'}) if gender == None: gender = 'None' else: gender = gender['class'][0] employment = soup.find('span', {'class': 'employment item'}) if employment == None: employment = 'None' else: employment = employment.string position = soup.find('span', {'class': 'position item'}) if position == None: position = 'None' else: position = position.string education = soup.find('span', {'class': 'education item'}) if education == None: education = 'None' else: education = education.string major = soup.find('span', {'class': 'education-extra item'}) if major == None: major = 'None' else: major = major.string temp = soup.find('img', {'alt': name}) avatar_url = temp['src'][0:-6] + temp['src'][-4:] agree = int( soup.find('span', { 'class': 'zm-profile-header-user-agree' }).strong.string) thanks = int( soup.find('span', { 'class': 'zm-profile-header-user-thanks' }).strong.string) infolist = soup.find_all('a', {'class': 'item'}) asks = int(infolist[1].span.string) answers = int(infolist[2].span.string) posts = int(infolist[3].span.string) try: collections = "" collections = int(infolist[4].span.string) except: print(infolist) try: logs = "" logs = int(infolist[5].span.string) except: print(infolist) followees = int(infolist[len(infolist) - 2].strong.string) followers = int(infolist[len(infolist) - 1].strong.string) scantime = int( soup.find_all( 'span', {'class': 'zg-gray-normal' })[len(soup.find_all('span', {'class': 'zg-gray-normal'})) - 1].strong.string) info = { 'name': name, 'uid': userID, 'location': location, 'business': business, 'gender': gender, 'employment': employment, 'position': position, 'education': education, 'major': major, 'agree': agree, 'thanks': thanks, 'asks': asks, 'answers': answers, 'posts': posts, 'collections': collections, 'logs': logs, 'followers': followers, 'scantime': scantime, } user_info.update(info) return info
def __init__(self): super(Account, self).__init__() self.session = BaseAPI.get_session()
def __init__(self): super(Logincontrol, self).__init__() self.session = BaseAPI.get_session()