如何用Python+人工识别处理知乎的倒立汉字验证码

xiaoxiao2021-02-27  125

  目前知乎采用了“倒立汉字”验证码,如图所示:

用户需要点击图片中所有的倒立汉字才能登陆知乎。

  这给Python爬虫的模拟登录带来了一定的难度,目前网络上的相关资料针对的都是普通的“英文+数字”验证码,针对“倒立汉字”验证码的文章较少。而且大家普遍采用的是requests库。经过几天的研究,我采用urllib.request实现了模拟登陆知乎,现将代码分享如下:

# 登录知乎,通过保存验证图片方式 import urllib.request import urllib.parse import time import http.cookiejar webUrl = "https://www.zhihu.com/login/email"#不能写https://www.zhihu.com/#signin因为不支持重定向 webheader = { # 'Accept': 'text/html, application/xhtml+xml, */*', # 'Accept-Language': 'zh-CN', # 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko', 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36', # 'User-Agent': 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5', # 'DNT': '1', # 'Connection': 'Keep-Alive' } postData = { 'email': '在这里写你的账号', 'captcha_type': 'cn', 'password': '在这里写你的密码', '_xsrf': '', 'captcha': '' } localStorePath = "写你想保存的验证码图片的地址" if __name__ == '__main__': #声明一个CookieJar对象实例来保存cookie cookie = http.cookiejar.CookieJar() #创建opener handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler)#建立opener对象,并添加头信息 urllib.request.install_opener(opener) captcha_url = 'https://www.zhihu.com/captcha.gif?r=%d&type=login&lang=cn' % (time.time() * 1000) # captcha_url = 'http://www.zhihu.com/captcha.gif?r=%d&type=login' % (time.time() * 1000)#这样获得的是“字母+数字验证码” #这个获取验证码图片的方法是不行的! # urllib.request.urlretrieve(captcha_url, localStorePath + 'myCaptcha.gif') #用urlopen函数保存验证图片 req = urllib.request.Request(url=captcha_url,headers=webheader) content = urllib.request.urlopen(req) # content = opener.open(req) captcha_name = 'D:/Python学习/crawler_learning/知乎登录专题研究/知乎验证码图片/myNewCaptcha.gif' content = content.read() with open(captcha_name, 'wb') as f: f.write(content) postData['captcha'] = input('请输入验证码') # postData['_xsrf'] = get_xsrf() postData['_xsrf'] = 'fa5ae712244bd4287e371801052003fc' print(postData['_xsrf']) #用urlopen函数传送数据给服务器实现登录 postData_encoded = urllib.parse.urlencode(postData).encode('utf-8') req = urllib.request.Request(url=webUrl,data=postData_encoded,headers=webheader) webPage = urllib.request.urlopen(req) # webPage = opener.open(req) data = webPage.read().decode('utf-8') print(data) with open("D:/知乎服务器反馈的内容.txt",mode='w',encoding='utf-8') as dataFile: dataFile.write(data)

网上的采用requests库实现的代码链接:http://www.jianshu.com/p/50c5815bb60b#

几点思考:

1、首先需要明确如何获得验证码图片的地址,利用Fiddler抓包获得的典型的验证码图片的地址如下:

https://www.zhihu.com/captcha.gif?r=1501679883167&type=login&lang=cn

这个“r”代表的是什么含义呢?经过查看知乎上的js代码可以确定,这个r指的是毫秒级的时间戳。

2、以验证码图片地址https://www.zhihu.com/captcha.gif?r=1501679883167&type=login&lang=cn为例,不同时间访问同一个验证码图片地址,得到的验证码图片是不同的,那么知乎服务器是如何知道你获取的是那张验证码呢?

我认为是通过sessionID,换句话说,知乎把某个验证码图片给了你,同时知乎记录下了你的sessionID和这个验证码的“正确答案”,这样将来你输入验证码给知乎后,知乎就能判断你输入的验证码是否正确了。

由于sessionID保存在cookie之中,所以Python模拟登陆的代码必须使用cookie。

3、获取验证码图片的时候,我用的是content =urllib.request.urlopen (req)函数,经过我的验证,用

urllib.request.urlretrieve函数是不行的,因为urlopen函数可以传递headers参数,而这一个参数必须有。

4、获得了倒立汉字图片以后,如何确定要传递给知乎的captcha是什么呢?经过Fiddler抓包,

传递的参数类似于这样:

{"img_size":[200,44],"input_points":[[43.44,22.44],[115.72,22.44]]}

经过分析和试验确定:200指的是图片长度,44指的是图片高度,后面的input_points指的是打在倒立汉字上的点的坐标。由于每次出现7个汉字,这7个汉字的坐标是固定的,我全部进行捕获:

{"img_size":[200,44],"input_points":[[12.95,14.969999999999998],[36.1,16.009999999999998],[57.16,24.44],[84.52,19.17],[108.72,28.64],[132.95,24.44],[151.89,23.380000000000002]]}

然后,问题就简单了:将图片保存在本地之后,打开图片,确定哪几个汉字倒立,比如说第2个和第6个,那就在上面选取出2和6的坐标输入即可,即

{"img_size":[200,44],"input_points":[[36.1,16.009999999999998],[132.95,24.44]]}。

5、小窍门:以验证码图片地址

https://www.zhihu.com/captcha.gif?r=1501679883167&type=login&lang=cn

去掉最后的&lang=cn或者换成&lang=en,验证码图片就不是倒立汉字了,就变成了普通的“字母+数字”组合了,http://www.jianshu.com/p/50c5815bb60b#就是采用的这种验证码图片,不过要注意的是,这个时候post的数据就要取消掉“'captcha_type': 'cn',”。

欢迎大家与我交流!

转载请注明原文地址: https://www.6miu.com/read-16196.html

最新回复(0)