首页 > 代码库 > 知乎登录设计思考

知乎登录设计思考

验证码

  技术分享

  2种方式:

    1) 我们在浏览器输入正确的账号和密码、验证码后。登录成功,记住我的登录信息是这个。我们接下来,写代码访问的时候在cookie中提交这个就可以了。过期时间是1个月。这个cookie中包含的信息是加密的,我们可以看到有个时间戳,保存的是登陆成功的时间。导致的问题,知乎后端肯定是会检查这个时间戳的,因此1个月后我们还要去正确登陆一下获取正确的cookie。这是值得我们学习的技巧,防爬策略,如果没有这个时间戳,就可以一直访问了,不用再登录。

  技术分享

    2)类似,只是这次我们使用代码来,获取验证码,然后人工识别,输入正确的验证码,登录。同样1个月后我们还要去重新获取一下正确的cookie值。

# coding=utf-8import sysimport urllibimport urllib2import cookielibimport timereload(sys)sys.setdefaultencoding(utf-8)# 创建一个cookie实例cookie = cookielib.CookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))headers = {User-Agent:           Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.25 Safari/537.36           }picture = opener.open(    https://www.zhihu.com/captcha.gif?r=%s&type=login % int(round(time.time() * 1000))).read()local = open(captcha.gif, wb)local.write(picture)local.close()captcha = raw_input(请输入验证码:)data = {_xsrf: 你的xsrf,可以在浏览器cookie中找到,不知道干吗用的,        password: xxxx,        captcha: captcha,        phone_num: xxxxx        }req = urllib2.Request(    https://www.zhihu.com/login/phone_num, urllib.urlencode(data), headers)res = opener.open(req)print(res.read().decode(unicode-escape).encode(utf-8))for c in cookie:    print(%s--%s % (c.name, c.expires))    c.expires = 1995962843for c in cookie:    print(%s--%s % (c.name, c.expires))req = urllib2.Request(https://www.zhihu.com/, headers=headers)res = opener.open(req)print(res.read())

  

  

 

知乎登录设计思考