Python3.4 12306 2015年3月验证码识别
import ssl
import json
from PIL import Image
import requests
import re
import urllib.request as urllib2
if hasattr(ssl, '_create_unverified_context'):
ssl.create_default_context = ssl._create_unverified_context
UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36"
pic_url = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand&0.21191171556711197"
def imgCut():
pic_obj = Image.open('./tmp.jpg')
box = (120, 0, 290, 25)
region = pic_obj.crop(box)
region.save('./text.jpg')
def ocrApi():
filename = './text.jpg'
upload_pic_url = "http://cn.docs88.com/pdftowordupload2.php"
filename_tmp = filename.split('/')[-1]
headers_fake = {
'ccept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
'Connection': 'keep-alive',
'Host': 'cn.docs88.com',
'Origin': 'http://cn.docs88.com',
'User-Agent': 'Mozilla/5.0 (KHTML, like Gecko) Chrome/41.0.2272.89',
'X-Requested-With': 'ShockwaveFlash/17.0.0.134',
}
para = {'Filename': filename_tmp,
'sourcename': filename_tmp,
'sourcelanguage': 'cn',
'desttype': 'txt',
'Upload': 'Submit Query',
}
upload_pic = requests.post(upload_pic_url, data=para, files={"Filedata" : open(filename, 'rb')}, headers=headers_fake) text_result_url = 'http://cn.docs88.com/' + str(upload_pic.content)[5:-1]
text_result = urllib2.urlopen(text_result_url).read().decode()
return text_result
def get_img():
resp = urllib2.urlopen(pic_url)
raw = resp.read()
with open('./tmp.jpg', 'wb') as fp:
fp.write(raw)
return Image.open('./tmp.jpg')
def get_sub_img(im, x, y):
assert 0 <= x <= 3
assert 0 <= y <= 2
#WITH = HEIGHT = 68
left = 5 + (67 + 5) * x
top = 41 + (67 + 5) * y
right = left + 67
bottom = top + 67
return im.crop((left, top, right, bottom))
def baidu_stu_lookup(im):
url = "http://stu.baidu.com/n/image?fr=html5&needRawImageUrl=true&id=WU_FILE_0&name=233.png&type=image%2Fpng&lastModifiedDate=Mon+Mar+16+2015+20%3A49%3A11+GMT%2B0800+(CST)&size="
im.save("./query_temp_img.png")
raw = open("./query_temp_img.png", 'rb').read()
url = url + str(len(raw))
req = urllib2.Request(url, raw, {'Content-Type': 'image/png', 'User-Agent': UA})
resp_url = urllib2.urlopen(req).read()
url = "http://stu.baidu.com/n/searchpc? queryImageUrl=" + urllib2.quote(resp_url)
req = urllib2.Request(url, headers={'User-Agent': UA})
resp = urllib2.urlopen(req)
html = resp.read().decode()
return baidu_stu_html_extract(html)
def baidu_stu_html_extract(html):
pattern = re.compile(r"keywords:'(.*?)'")
matches = pattern.findall(html)
if not matches:
return '[UNKOWN]'
json_str = matches[0]
json_str = json_str.replace('\\x22', '"').replace('\\\\', '\\')
result = [item['keyword'] for item in json.loads(json_str)]
return '|'.join(result) if result else '[UNKOWN]'
if __name__ == '__main__':
im = get_img()
imgCut()
captcha_text = ocrApi()
print(captcha_text)
dic_list = {}
count = 0
for y in range(2):
for x in range(4):
count += 1
im2 = get_sub_img(im, x, y)
result = baidu_stu_lookup(im2)
dic_list[count] = result
print((y, x), result)
if captcha_text.strip():
print('\n可能的结果是:')
maybe_result = []
for v in dic_list:
for c in range(len(captcha_text.strip())):
text = (captcha_text)[c]
if text in dic_list[v]:
_str_res = '%s --- %s' % (v, dic_list[v])
maybe_result.append(_str_res)
for r in list(set(maybe_result)):
print(r)
else:
print('False')
<span style="font-family: Arial, Helvetica, sans-serif;">改自 https://gist.github.com/Evi1m0/fbbdb1ba7c66cc4e1bb2</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><h2 style="font-family: 'Microsoft Yahei', sans-serif; margin: 0px; padding: 0px; line-height: 26px;">转载请注明作者与出处:<a target=_blank target="_blank" href="http://blog.csdn.net/u013511642" style="color: rgb(202, 0, 0); text-decoration: none;">http://blog.csdn.net/u013511642</a> 王小涛_同學</h2>
Python3.4 12306 2015年3月验证码识别的更多相关文章
- 我的Python成长之路---第一天---Python基础(作业2:三级菜单)---2015年12月26日(雾霾)
作业二:三级菜单 三级菜单 可一次进入各个子菜单 思路: 这个题看似不难,难点在于三层循环的嵌套,我的思路就是通过flag的真假来控制每一层的循环的,简单来说就是就是通过给每一层循环一个单独的布尔变量 ...
- 我的Python成长之路---第一天---Python基础(作业1:登录验证)---2015年12月26日(雾霾)
作业一:编写登录接口 输入用户名密码 认证成功系那是欢迎信息 输错三次后锁定 思路: 1.参考模型,这个作业我参考了linux的登录认证流程以及结合网上银行支付宝等锁定规则 1)认证流程参考的是Lin ...
- 2015年3月阿里内推(c++研发)实习生电面经历
2015年3月开学开始就听说阿里会有内推,果不其然在师兄的引荐下推了菜鸟网络,但是在学校的BBS上看到了阿里云部门,而且要会C++,这使我更感兴趣,重新选择了方向,当然最后选择了阿里云.在此分享一下三 ...
- TIOBE 2015年7月编程语言排行榜:C++的复兴
TIOBE 2015年7月编程语言排行榜:C++的复兴 发表于2015-07-13 17:03| 16086次阅读| 来源CSDN| 128 条评论| 作者钱曙光 编程语言排行榜TIOBEC++ 摘要 ...
- 2015年12月28日 Java基础系列(六)流
2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流
- 2015年12月13日 spring初级知识讲解(四)面向切面的Spring
2015年12月13日 具体内容待补充...
- 2015年8月18日,杨学明老师《技术部门的绩效管理提升(研讨会)》在中国科学院下属机构CNNIC成功举办!
2015年8月18日,杨学明老师为中国网络新闻办公室直属央企中国互联网络中心(CNNIC)提供了一天的<技术部门的绩效管理提升(研讨会)>培训课程.杨学明老师分别从研发绩效管理概述.研发绩 ...
- 2015年8月17日,杨学明老师《产业互联网化下的研发模式转型》在中国科学院下属机构CNNIC成功举办!
2015年8月17日,杨学明老师为中国网络新闻办公室直属央企中国互联网络中心(CNNIC)提供了一天的<产业互联网化下的研发模式转型>内训课程.杨学明老师分别从产业互联网化的问题与挑战.传 ...
- 【C++】命令行Hangman #2015年12月15日 00:20:27
增加了可以在构造Hangman对象时通过传入参数设定“最大猜测次数”的功能.少量修改.# 2015年12月15日 00:20:22 https://github.com/shalliestera/ha ...
随机推荐
- vue工程化之项目引入jquery
既然写项目,那么少不了用jq,那我们就引入进来吧 1.因为已经安装了vue脚手架,所以需要在webpack中全局引入jquery 打开package.json文件,在里面加入这行代码,jquery后面 ...
- webstorm 设置ES6语法支持以及添加vuejs开发配置
参考文章:https://blog.csdn.net/diligentkong/article/details/75040651
- Vue+Bootstrap实现购物车程序(1)
先看下案例效果:(简单的数量控制及价格运算) 代码: <!DOCTYPE html> <html> <head lang="en"> <m ...
- zabbix4.2学习笔记--监控nginx
图解一个客户端连接开源版本的Nginx情况 Accepts(接受).Handled(已处理).Requests(请求数)是一直在增加的计数器.Active(活跃).Waiting(等待).Readin ...
- 服务器端编程心得(二)—— Reactor模式
最近一直在看游双的<高性能linux服务器编程>一书,下载链接: http://download.csdn.net/detail/analogous_love/9673008 书上是这么介 ...
- MYSQL每日一学 - 时间间隔表达式
参考链接:https://dev.mysql.com/doc/refman/5.7/en/expressions.html Interval表达式(Temporal intervals)的使用 Int ...
- MySql 基础 基本使用方法
安装MySQL linux安装:阿里云服务器ecs配置之安装mysqlwindows安装: 解压 管理员身份进cmd执行解压目录下的可执行文件 初始化 D:\mysql-8.0.12-winx64\m ...
- Windows Server 2008 R2 Enterprise 安装.NET Framework 4.0
由于服务器上没有.NET 4.0 部署不了 4.0及以上版本的网站,所以给他安排一下: 复制下好的.NET Framework 4.0到服务器开始安装: 安装完,重新打开IIS,已经 ...
- 九度oj 题目1206:字符串连接
题目1206:字符串连接 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:5117 解决:2373 题目描述: 不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来 ...
- nyoj 2 括号配对问题(stack)
括号配对问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在,有一行括号序列,请你检查这行括号是否配对. 输入 第一行输入一个数N(0& ...