Python3.4 12306 verification code identification, python3.412306

Source: Internet
Author: User

Python3.4 12306 verification code identification, python3.412306

<Pre name = "code" class = "python"> import sslimport jsonfrom PIL import Imageimport requestsimport reimport urllib. request as urllib2if hasattr (ssl, '_ create_unverified_context'): ssl. create_default_context = ssl. _ create_unverified_contextUA = "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/p AsscodeNew/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-color': 'zh-CN, Zh; q = 0.8, en; q = 0.6 ', 'connection': 'Keep-alive', 'host': 'cn.doc s88.com', 'origin': 'http: // cn.docs88.com ', 'user-agent': 'mozilla/5.0 (KHTML, like Gecko) Chrome/41.0.2272.89', 'x-Requested-': '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_u Rl, 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_resultdef 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? Metric & type = image % 2 Fpng & 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/se Archpc? 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 possible result:') 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 ')



Change from https://gist.github.com/Evi1m0/fbbdb1ba7c66cc4e1bb2
Python3.4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.