Import sslimport jsonfrom PIL import imageimport urllibimport reimport urllib.request as Urllib2if hasattr (SSL, ' _create_u Nverified_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.1 2306.cn/otn/passcodenew/getpasscodenew?module=login&rand=sjrand&0.21191171556711197 "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 + (5) * x top = + (+ 5) * Y right = left + bottom = top + Im.crop return (left, t OP, right, bottom)) def baidu_stu_lookup (im): url = "http://stu.baidu.com/n/image?fr=html5&needRawImageUrl=true& Amp;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 '). The 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 = "Htt P://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_st R.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 () for y in rangE (2): For x in range (4): im2 = Get_sub_img (IM, x, y) result = Baidu_stu_lookup (IM2) Print ((y, x), result)
Change from https://github.com/andelf/fuck12306/blob/master/fuck12306.py
Python 3.4 Available
Python3.4 12306 March 2015 Verification Code identification