试了好久登录的时候总是显示:系统忙,请刷新,,,太折磨人了,搞了半天才想到是请求头部的问题.....
验证码还是要人工识图..
#!/bin/env python # -*- coding=utf-8 -*- import ssl import sys import urllib2 import random import httplib import json from cookielib import LWPCookieJar import urllib import re import getpass reload(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_context codeimg = 'https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand&%s' % random.random() baner = """ ################################## 12306登录脚本,作者Mr RJL python版本:2.7,适用于linux 验证码输入方式: 输入问题对应的图片序号,1-8; 多个以','分隔.如: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' pass def 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 cookietp(): stoidinput("获取Cookie") Url = "https://kyfw.12306.cn/otn/login/init" get(Url) for index, c in enumerate(cookiejar): stoidinput(c) def getImg(): stoidinput("下载验证码...") 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 pass def stoidinput(text): """ 正常信息输出 :param text: :return: """ print "\033[34m[*]\033[0m %s " % text def errorinput(text): """ 错误信息输出 :param text: :return: """ print "\033[32m[!]\033[0m %s " % text return False def codexy(): """ 获取验证码 :return: str """ Ofset = raw_input("[*] 请输入验证码: ") select = Ofset.split(',') global randCode post = [] offsetsX = 0 # 选择的答案的left值,通过浏览器点击8个小图的中点得到的,这样基本没问题 offsetsY = 0 # 选择的答案的top值 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 = { "_json_att": None } fresult = json.loads(Post(randurl, randdata), encoding='utf8') checkcode = fresult['data']['msg'] if checkcode == 'FALSE': errorinput("验证码有误,请重试") else: stoidinput("验证码通过,开始登录..") try: tresult = json.loads(Post(logurl, logdata), encoding='utf8') if tresult['data'].__len__() == 0: errorinput("登录失败: %s" % tresult['messages'][0]) else: stoidinput("登录成功") sult = Post(surl, ldata) getUserinfo() except ValueError as e: errorinput(e) def getUserinfo(): """ 登录成功后,显示用户名 :return: """ url = 'https://kyfw.12306.cn/otn/modifyUser/initQueryUserInfo' data = dict(_json_att=None) result = Post(url, data) userinfo = result name = r'<input name="userDTO.loginUserDTO.user_name" style="display:none;" type="text" value="(\S+)" />' try: stoidinput("欢迎 %s 登录" % re.search(name, result).group(1)) except AttributeError: pass def main(): user = raw_input("[+] 用户名(用户名/邮箱/手机): ") passwd = getpass.getpass("[+] 密码: ") if user == '' or passwd == '': errorinput("用户名或密码不能为空!") else: cookietp() getImg() codexy() login(user, passwd) def logout(): url = 'https://kyfw.12306.cn/otn/login/loginOut' result = get(url) if result: stoidinput("已退出") else: errorinput("退出失败") if __name__ == "__main__": print baner main() logout()
The above is the detailed content of Use python crawler to simulate 12306 login method. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
