xchaoinfo / fuck-login

模拟登录一些知名的网站,为了方便爬取需要登录的网站
5.86k stars 2.02k forks source link

请问知乎验证码中的t是如何发现的? #31

Open Rockyzsu opened 8 years ago

Rockyzsu commented 8 years ago

之前做过知乎登录,无奈验证码的那个调用的字符不知道怎么获取。 就是 http://www.zhihu.com/captcha.gif?r=1471341285051&type=login 中 r=xxxxxxxxxx 中的数字,看了你的源码才明白跟时间有关的。

亲问楼主,这个数字的来源是通过自己的经验和判断得出来的吗 ? 还是在源码或者抓包中找到的?

谢谢!

xchaoinfo commented 8 years ago

主要是通过抓包分析和经验判断了,当然也可以通过阅读 js 源码发现这个情况。

Rockyzsu commented 8 years ago

@xchaoinfo 谢谢大神的回答~

liuhuang9492 commented 8 years ago

我想问一下楼主,你这个是怎么保证你代码中的POST请求的时候访问的URL的验证码和你之前是验证码是一致的呢?根据代码中的时间戳吗?但是我借用了楼主代码中的中的时间戳部分还是不行,楼主能否帮忙看一下代码(部分):

def input_captcha(self):
    t = str(int(time.time() * 1000))
    self.captcha_url = 'https://www.zhihu.com/captcha.gif?r'+t+'&type=login'
    print self.captcha_url
    captcha_name = 'captcha.gif'
    r = requests.get(self.captcha_url,headers=self.header)
    with open(captcha_name,'wb') as captcha_file:
        captcha_file.write(r.content)
    print "="*50
    print "已经打开验证码图片,请输入验证码:"
    subprocess.call(captcha_name,shell=True)
    captcha_num = raw_input("请输入验证码:")
    os.remove(captcha_name)
    return captcha_num

def login(self,username='',passwd=''):

    self.username = username
    self.password = passwd
    data = {
        "_xsrf":self.get_XSRF(),
        "password":self.password,
        "remember_me":"true",
        "email":self.username,
        "captcha":self.input_captcha(),
    }
    session = requests.session()
    r =session.post(self.login_url, headers=self.header, data=data)
    print "="*50
    print r.status_code
    print r.content
xchaoinfo commented 8 years ago

session 要是全局变量才可以的,session 的运用在于自动处理 cookies 类初始化的时候用 self.session = requests.session()

后面的请求都用 self.session 来写

liuhuang9492 commented 8 years ago

@xchaoinfo 多谢楼主.。

Hankertanker commented 8 years ago

大神,我在成功登陆后再次登录,提示您已成功登录,但是后面有报错,能解释下什么原因吗?

您已经登录 Traceback (most recent call last): File "zhifu.py", line 139, in login(secret, account) NameError: name 'secret' is not defined

xchaoinfo commented 8 years ago

@Hankertanker

  1. 请确保你的代码是,在 github 下载的最新的版本,而不是复制其他地方的。
  2. 您的 Python 版本最好是 2.7.x 或者 3.4+ 的版本。
  3. 如果问题仍然存在,你可以把你下载的代码和运行出现问题的截图,发邮件到 xchaoinfo@qq.com , 我们做一个更深入的交流。
hitao123 commented 7 years ago

楼主,请教一个问题,现在知乎在登录成功之后并没有返回json数据

环境

python 2.7.13 windows 10 64位

def log_in(email, password):
    login_url = 'https://zhihu.com/login/email'
    data = {
        'email': email,
        'password': password,
        '_csrf': get_xsrf(),
        'captcha': get_captcha()
    }
    response = session.post(login_url, data = data, headers = headers)
    login_data = response.json()
    if login_data['r'] != 1:
        print 'Login failed'
    else:
        for i in login_data:
            print i
    session.cookies.save()

出现 ValueError: No JSON object could be decoded, 正常手动登录查看,发现并没有返回json数据,这个怎么解决

xchaoinfo commented 7 years ago

@hitao123 你使用的是 最新的 代码吗? 我这里在 Python3.4 下,测试没有出现你的问题,你重复多试几次,是否是由于网络问题造成的呢。 还有 我github 的代码是 Py2 和 Py3 版本兼容的,理论上你不做任何修改就能使用的。 还有一点就是,我的 User-Agent 使用的 android 的。

hitao123 commented 7 years ago

不是网络原因,看到了 User-Agent 使用的是 android,这和使用浏览器 UA有什么区别呢?

response = session.post(login_url, data = data, headers = headers)
login_data = response.json()

关键是这里 response.json() 方法不能使用,根本没有返回 json数据

这是完整的报错信息

    login_data = response.json()
  File "C:\Python27\lib\site-packages\requests\models.py", line 866, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Python27\lib\json\__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
xchaoinfo commented 7 years ago

@hitao123 我在 Py2.7 进行了测试,没有出现你的问题,你把代码发到 xchaoinfo@qq.com 吧, 由于我这里没法复现你的问题。