首页 > 我在使用python登陆知乎如何输入确定验证码,代码是我在社区中搜到的,我实验了一下,求解释一下错误

我在使用python登陆知乎如何输入确定验证码,代码是我在社区中搜到的,我实验了一下,求解释一下错误

import requests
import re
import time
from subprocess import Popen


headers ={
     'Accept':'*/*' ,
     'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
     'X-Requested-With':'XMLHttpRequest',
     'Referer':'http://www.zhihu.com',
     'Accept-Language':'zh-CN',
     'Accept-Encoding':'gzip, deflate',
     'User-Agent':'Mozilla/5.0(Windows NT 6.1;WOW64;Trident/7.0;rv:11.0)like Gecko',
     'Host':'www.zhihu.com'
     }

s =requests.session()
r = s.get('http://www.zhihu.com',headers =headers)
def getXSRF(r):
    cer = re.compile('name=\"_xsrf\" value=\"(.*)\"', flags = 0)
    strlist = cer.findall(r.text)
    return strlist[0]
_xsrf =getXSRF(r)

print(r.request.headers)
print(str(int(time.time()*1000)))
Captcha_URL= 'http://www.zhihu.com/captcha.gif?r='+ str(int(time.time()*1000))
r = s.get(Captcha_URL,headers =headers)

with open('code.gif','wb') as f:
    f.write(r.content)
Popen('code.gif',shell =True)
captcha =input('captcha: ')
login_data = {
    '_xsrf':_xsrf,
    'email':'xxxxx@xxxx.com',
    'password': 'xxxxxxxx,
    'remember_me':'true',
    'captcha':captcha
}

r = s.post('http://www.zhihu.com/login/email',data=login_data,headers=headers)
print(r.text)
r = s.get('http://www.zhihu.com/settings/profile')
print(r.text)


{'Accept-Language': 'zh-CN', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'Mozilla/5.0(Windows NT 6.1;WOW64;Trident/7.0;rv:11.0)like Gecko', 'Host': 'www.zhihu.com', 'Referer': 'http://www.zhihu.com', 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
1445417491333
captcha: "fjkm"
{
    "r": 1,
    "errcode": 1991829,
    
    "data": {"captcha":"\u8bf7\u63d0\u4ea4\u6b63\u786e\u7684\u9a8c\u8bc1\u7801 :("},
    
    
    "msg": "\u8bf7\u63d0\u4ea4\u6b63\u786e\u7684\u9a8c\u8bc1\u7801 :("
    
}
<!doctype html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>知乎</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="http://static.zhihu.com/static/css/desktop/errors.css" />
  <link rel="shortcut icon" href="http://static.zhihu.com/static/img/favicon.ico" />
</head>
<body>

<div class="page">
  <div class="error error-wide">
    <div class="header">
      <a href="/" id="page_logo" class="fl">
        <img src="http://static.zhihu.com/static/img/logo_black_trans.png" alt="知乎">
      </a>
      <strong class="mute">一个真实的问答社区</strong>
    </div>
    <div class="content">
      <p class="single">服务器提了一个问题,我们正在紧张地撰写答案...</p>
    </div>
  </div>
</div>
<script src="http://static.zhihu.com/static/js/desktop/500.js"></script>
</body>
</html>

请问你在登陆时,获取到的验证码是 "fjkm" 吗?

那个返回的信息是在告诉你, 你输入的验证码不正确.

with open('code.gif','wb') as f:
    f.write(r.content)
Popen('code.gif',shell =True)
captcha =input('captcha: ')

代码在下载好验证码之后, 保存的文件名是 code.gif, 你需要根据这个图片上的验证码, 输入相应的内容,
而你直接就输入 "fjkm", 怎么可能对?


我的机器人原来就是这样挂掉的.以前知乎是不用输验证码的.

【热门文章】
【热门文章】