Python crawler: Analog 12306 Login

Source: Internet
Author: User

Tried for a long time to log on always show: System busy, please refresh,,, too tortured people, engaged for a half a day to think is the question of the request head ....

Verification code or to manually identify the diagram.

#!/bin/env python# -*- coding=utf-8 -*-import sslimport sysimport  urllib2import randomimport httplibimport jsonfrom cookielib import  Lwpcookiejarimport urllibimport reimport getpassreload (SYS) sys.setdefaultencoding (' UTF8 ') Cookiejar = lwpcookiejar () cookiesuppor = urllib2. Httpcookieprocessor (Cookiejar) Opener = urllib2.build_opener (cookiesuppor, urllib2. HttpHandler) Urllib2.install_opener (opener) ssl._create_default_https_context = ssl._create_unverified _contextcodeimg =  ' Https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand &%s '  % random.random () baner =  "" "##################################     12306 Login Script, author Mr rjl    python version: 2.7, for linux     verification Code Input Method:      input problem corresponds to the picture ordinal,1-8;     multiple with ', ' delimited. For example: 1,2,3################################## "" "Def get (URL):    try:         request = urllib2. Request (Url=url)         # req.add_header (' User-Agent ',  ' mozilla/5.0  (x11; fedora; linux x86_64; rv:52.0)  gecko/20100101 firefox/ 52.0 ')         request.add_header ("Content-type",  "application/ X-www-form-urlencoded; charset=utf-8 ")         request.add_header ( ' X-requested-with ',  ' XMLHttpRequest ')         request.add_header (' User-agent ',                             ' mozilla/5.0  (windows nt 6.1)  AppleWebKit/537.36  (Khtml, like gecko)  chrome/33.0.1750.154 safari/537.36 ')         request.add_header (' Referer ',  ' https:// Kyfw.12306.cn/otn/login/init ')         request.add_header (' Accept ',   ' */* ')         result = urllib2.urlopen (Request). Read ()         assert isinstance (Result, object)          return result    except httplib.error as  e:        print e         pass    except urllib2. urlerror as e:        print e         pass    except urllib2. Httpbasicauthhandler, urllib2. httperror:        print  ' ERROR '          passdef post (Url, data):    try:         request = urllib2. Request (Url=url, data=urllib.urlencode (data))         #  Req.add_header (' user-agent ',  ' mozilla/5.0  (x11; fedora; linux x86_64; rv:52.0)  gecko/20100101 firefox/52.0 ')         # request =  urllib2. Request (Ajax_url, urllib.urlencode (DC))         request.add_header ("Content-type",  "Application/x-www-form-urlencoded; charset=utf-8")          request.add_header (' X-requested-with ',  ' XMLHttpRequest ')          request.add_header (' user-agent ',                             ' mozilla/5.0  (windows nt 6.1)  AppleWebKit/ 537.36  (Khtml, like gecko)  chrome/33.0.1750.154 safari/537.36 ')          request.add_header (' Referer ',  ' https://kyfw.12306.cn/otn/login/init ')          request.add_header (' Accept ',  ' */* ')          # request.add_header (' accept-encoding ',  ' gzip, deflate ')          result = urllib2.urlopen (Request). Read ()          return result    except httplib.error as e:         return e    except urllib2. urlerror as e:        return e     Except urllib2. HttpbasicauthhAndler, urllib2. httperror:        return  ' ERROR ' DEF&NBSP;COOKIETP ():     stoidinput ("Get Cookie")     Url =  "https://kyfw.12306.cn/otn/login/ Init "    get (URL)     for index, c in enumerate ( Cookiejar):         stoidinput (c) def getimg ():     stoidinput ("Download Verification Code ...")     result = get (codeimg)     try :         if open ('/tmp/tkcode ',  ' WB '). Write (Result)  :             import os             os.system ("oeg /tmp/tkcode &")          else:            return false    except oserror as e:         print e        passdef stoidinput (text):      ""      normal information output     :p aram text:      :return:      ""     print  "\033[34m[*]\033[0m"  %s  " % textdef errorinput (text):    " "" "     Error message output     :p aram text:     :return:       ""     print  "\033[32m[!" \033[0m %s  " % text    return falsedef codexy ():      "" "     Get Verification Code     :return: str    " ""         ofset = raw_Input ("[*]  Please enter Verification code: ")     select = ofset.split (', ')      global randcode    post = []    offsetsx =  0  #  Select the left value of the answer by clicking on the midpoint of the 8 small graphs in the browser to get it, so basically no problem     offsetsy = 0   #  top value of the selected answer     for ofset in select:         if ofset ==  ' 1 ':             offsetsY = 46             offsetsx = 42        elif ofset ==   ' 2 ':             offsetsy = 46             offsetsX = 105         elif ofset ==  ' 3 ':             offsetsy = 45            offsetsx =  184        elif ofset ==  ' 4 ':             offsetsY = 48             offsetsX = 256         elif ofset ==  ' 5 ':             offsetsy = 36            offsetsx  = 117        elif ofset ==  ' 6 ':             offsetsY = 112              offsetsx = 115        elif ofset  ==  ' 7 ':             offsetsy =  114            offsetsx = 181         elif ofset ==  ' 8 ':             offsetsY = 111             offsetsX = 252         else:            pass         post.append (OFFSETSX)         post.append ( Offsetsy)     randcode = str (POST). Replace ('] ',  '). Replace (' [',  '). Replace ("'" ", "). Replace ('   ', ") Def login (user, passwd):    randurl =  ' https://kyfw.12306.cn/otn/ Passcodenew/checkrandcodeansyn '     logurl =  ' https://kyfw.12306.cn/otn/login/ Loginaysnsuggest '     surl =  ' https://kyfw.12306.cn/otn/login/userLogin '      geturl =  ' https://kyfw.12306.cn/otn/index/initMy12306 '      randdata = {         "Randcode": randCode,          "Rand":  "Sjrand"     }     logdata = {         "Loginuserdto.user_name":  user,          "Userdto.password": passwd,          "Randcode": randcode    }    ldata =  {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;   "_json_att": none    }    fresult =  Json.loads (Post (randurl, randdata),  encoding= ' UTF8 ')     checkcode =  fresult[' data ' [' msg ']    if checkcode ==  ' FALSE ':         errorinput ("Verification code is wrong, please try again")     else:         stoidinput ("Verification code passed, start login ...")         try:             tresult = json.loads (Post (logurl, logdata),  encoding= ' UTF8 ')              if tresult[' data '].__len__ ()  == 0:                 errorinput (" Login failed:  %s " % tresult[' Messages '][0])             else:                 stoidinput ("Login Successful")                  sult = post (Surl, ldata)                  getuserinfo ()          except ValueError as e:             errorinput (e) def getuserinfo ():     "" "     login successful, display user name     :return:     ""     url =  ' https:// Kyfw.12306.cn/otn/modifyuser/initqueryuserinfo '     data = dict (_json_att=None)     result = post (Url, data)     userinfo =  Result   &nbsP;name = r ' <input name= "UserDTO.loginUserDTO.user_name"  style= "Display:none;"  type= "Text"  value= "(\s+)"  /> "    try:         stoidinput ("Welcome  %s  Login"  % re.search (name, result). Group (1))      except attributeerror:        passdef main ():     user = raw_input ("[+]  User name (username/email/mobile): ")      passwd = getpass.getpass ("[+]  password: ")     if user ==   '  or passwd ==  ':         errorinput (" User name or password cannot be empty! ") &NBSP;&NBSP;&NBSP;&NBSP;ELSE:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;COOKIETP ()          getimg ()         codexy ()          loGin (USER,&NBSP;PASSWD) def logout ():    url =  ' https://kyfw.12306.cn/otn/ Login/loginout '     result = get (URL)     if result:         stoidinput ("exited")     else:         errorinput ("Exit failed") if __name__ ==  "__main__":     print baner    main ()     logout ()


This article from "Lonely No Worry" blog, please be sure to keep this source http://jlnetc.blog.51cto.com/10920149/1907446

Python crawler: Analog 12306 Login

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.