12306抢票系统(登录功能---二维码+账号密码)
文章目录
- 一、二维码登录方式
- 二、账号密码登录方式
- 超级鹰第三方库
- 账号密码登录
- 代码汇总:
一、二维码登录方式
二维码登录方式:用户手机端扫描网页二维码登录,比账号密码登录更简易
话不多说,先上代码:
initmyUrl = 'https://kyfw.12306.cn/otn/view/index.html'
script = 'Object.defineProperty(navigator,"webdriver",{get:() => false,});' # 修改navigator属性,避免爬虫被12306发现
def Login(account, password, from_station, to_station, isStudent, timelst, seat_request, to_day, name,erweima):if erweima == 1:web = webdriver.Chrome(R"\chromedriver.exe") # 根据自身配置chromedriver位置web.maximize_window()web.get("https://kyfw.12306.cn/otn/resources/login.html") # 登录网站web.execute_script(script)while True: # 自旋等待用户扫描二维码成功并跳转登录界面if web.current_url != initmyUrl:time.sleep(1)else:print('登录成功!')breakelse: # 账号密码登录方式、将在下面展示
代码实现结果为:
TIPS:如还有疑问,欢迎留言
二、账号密码登录方式
超级鹰第三方库
超级鹰官网链接(点击跳转)
为什么要用到这个第三方库呢?原因就是12306那连人都难以分辨的验证码
接下来我将介绍怎么使用这个第三方库
其中代码为:
#!/usr/bin/env python
# coding:utf-8import requests
from hashlib import md5class Chaojiying_Client(object):def __init__(self, username, password, soft_id):self.username = usernamepassword = password.encode('utf8')self.password = md5(password).hexdigest()self.soft_id = soft_idself.base_params = {'user': self.username,'pass2': self.password,'softid': self.soft_id,}self.headers = {'Connection': 'Keep-Alive','User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',}def PostPic(self, im, codetype):"""im: 图片字节codetype: 题目类型 参考 http://www.chaojiying.com/price.html"""params = {'codetype': codetype,}params.update(self.base_params)files = {'userfile': ('ccc.jpg', im)}r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)return r.json()def ReportError(self, im_id):"""im_id:报错题目的图片ID"""params = {'id': im_id,}params.update(self.base_params)r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)return r.json()if __name__ == '__main__':chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '申请的软件ID')im = open('验证码图片', 'rb').read()print(chaojiying.PostPic(im, 9004)) #9004为自定义端口号,通过12306验证码可使用9004端口
账号密码登录
通过使用超级鹰第三方库,我们实现了验证码的校验,如何实现接下来的滑块验证呢,请看接下来的代码:
else:t = 5 # 由于第二次的验证码难度较大,我选择在验证失败后抛出异常,重新进入登录界面进行验证while t: t -= 1try:web = webdriver.Chrome(R"\chromedriver.exe") # 根据自身配置chromedriver位置web.maximize_window()web.get("https://kyfw.12306.cn/otn/resources/login.html") # 登录网站web.execute_script(script) # 避免爬虫被发现time.sleep(0.5)web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click() # 点击对应的账号密码登录按钮time.sleep(0.5)web.get_screenshot_as_file(r"new_image.png") # 截取整个页面imgelement = web.find_element_by_xpath('//*[@id="J-loginImg"]') # 找到图片验证码的实际位置rangle = (1252, 360, 1652, 595) # 需要根据自身电脑配置相对位置!!!!i = Image.open("new_image.png")frame4 = i.crop(rangle) # 从截图中再次截取我们需要的区域frame4 = frame4.convert('RGB') # 渲染图片frame4.save('check_image.jpg') # 保存为check_image图片web.find_element_by_xpath('//*[@id="J-userName"]').send_keys(account) # 输入账号密码web.find_element_by_xpath('//*[@id="J-password"]').send_keys(password)im = open('check_image.jpg', 'rb').read() # 解析图片,返回地址dic = chaojiying.PostPic(im, 9004)result = dic['pic_str']print(result)rs_list = result.split("|")time.sleep(0.5)action = ActionChains(web) # 鼠标移位并点击for rs in rs_list:p_temp = rs.split(",")x = int(p_temp[0])-58 # 需要根据自身电脑配置相对位置!!!!y = int(p_temp[1])-33print(x, y)action.move_to_element_with_offset(imgelement, x, y).click().perform()action = ActionChains(web)web.find_element_by_xpath('//*[@id="J-login"]').click() # 点击按钮登录time.sleep(3)btn = web.find_element_by_xpath('//*[@id="nc_1_n1z"]') # 滑块验证码ActionChains(web).drag_and_drop_by_offset(btn, 300, 0).perform()time.sleep(3)breakexcept Exception as e: # 验证码错误即重新登录web.close()print('验证码校验失败!正在重新登录')
改进方式:读者也可直接通过Xpath爬取当前界面的验证码图片,这样稍微简单点,博主懒癌犯了,请允许我划划水
结果展示:
代码汇总:
import time
import json
from PIL import Image
from selenium import webdriver
from chaojiying import Chaojiying_Client
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChainschaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '软件ID')
initmyUrl = 'https://kyfw.12306.cn/otn/view/index.html'
script = 'Object.defineProperty(navigator,"webdriver",{get:() => false,});' # 修改navigator属性,避免爬虫被12306发现
def Login(account, password, from_station, to_station, isStudent, timelst, seat_request, to_day, name,erweima):if erweima == 1:web = webdriver.Chrome(R"\chromedriver.exe") # 根据自身配置chromedriver位置web.maximize_window()web.get("https://kyfw.12306.cn/otn/resources/login.html") # 登录网站web.execute_script(script)while True: # 自旋等待用户扫描二维码成功并跳转登录界面if web.current_url != initmyUrl:time.sleep(1)else:print('登录成功!')breakelse:t = 5 # 由于第二次的验证码难度较大,我选择在验证失败后抛出异常,重新进入登录界面进行验证while t: t -= 1try:web = webdriver.Chrome(R"\chromedriver.exe") # 根据自身配置chromedriver位置web.maximize_window()web.get("https://kyfw.12306.cn/otn/resources/login.html") # 登录网站web.execute_script(script) # 避免爬虫被发现time.sleep(0.5)web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click() # 点击对应的账号密码登录按钮time.sleep(0.5)web.get_screenshot_as_file(r"new_image.png") # 截取整个页面imgelement = web.find_element_by_xpath('//*[@id="J-loginImg"]') # 找到图片验证码的实际位置rangle = (1252, 360, 1652, 595) # 需要根据自身电脑配置相对位置!!!!i = Image.open("new_image.png")frame4 = i.crop(rangle) # 从截图中再次截取我们需要的区域frame4 = frame4.convert('RGB') # 渲染图片frame4.save('check_image.jpg') # 保存为check_image图片web.find_element_by_xpath('//*[@id="J-userName"]').send_keys(account) # 输入账号密码web.find_element_by_xpath('//*[@id="J-password"]').send_keys(password)im = open('check_image.jpg', 'rb').read() # 解析图片,返回地址dic = chaojiying.PostPic(im, 9004)result = dic['pic_str']print(result)rs_list = result.split("|")time.sleep(0.5)action = ActionChains(web) # 鼠标移位并点击for rs in rs_list:p_temp = rs.split(",")x = int(p_temp[0])-58 # 需要根据自身电脑配置相对位置!!!!y = int(p_temp[1])-33print(x, y)action.move_to_element_with_offset(imgelement, x, y).click().perform()action = ActionChains(web)web.find_element_by_xpath('//*[@id="J-login"]').click() # 点击按钮登录time.sleep(3)btn = web.find_element_by_xpath('//*[@id="nc_1_n1z"]') # 滑块验证码ActionChains(web).drag_and_drop_by_offset(btn, 300, 0).perform()time.sleep(3)breakexcept Exception as e: # 验证码错误即重新登录web.close()print('验证码校验失败!正在重新登录')
博主将持续更新,欢迎关注!
12306抢票系统(登录功能---二维码+账号密码)相关推荐
- 12306抢票系统(框架+代码)- 持续更新
文章目录 一.框架展示(后续将提供源码) 二.界面展示 三.过程及结果展示 12306抢票系统(登录功能-二维码+账号密码)---------- 点击跳转 一.框架展示(后续将提供源码) 首先在 ...
- 跨端扫码确认实现Web登录(扫二维码登录)
起初的想法是类似于QQ扫码登录,BILIBILI扫码登录一样,通过手机确认后,在web端重定向完成登录 通过对BILIBILI扫码功能的解析,自己实现了一套类似扫码登录的功能 以下为伪代码,仅供查阅 ...
- 微信扫码登录自定义二维码样式
微信扫码登录自定义二维码样式 前言 Java生成data-url 1.工具类pom 2.代码实现 将data-url赋值给href 前言 今天在做web端扫码登录时,前端需要定义二维码的样式.官方文档 ...
- 微信扫码登录自定义二维码显示信息
微信扫码登录,自定义二维码显示信息 <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js& ...
- Mac pro微信开发者工具无法显示登录的二维码解决方案
一开始在微信小程序里是使用测试号开发的,更改测试号为自己的AppId死活改不了,报错: Error: 需要重新登录,access_token expired[touristappid] 1.首先要确保 ...
- 微信活码系统/微信群二维码/活码生成系统/生成微信活码
微信活码系统/微信群二维码/活码生成系统/生成微信活码 前些日子还有朋友在找这个来着.现在有了,自己部署个活码用就是了,这个大概就是,死了网址不死码的意思吧. 对这一类没什么研究,实测过了,东西没问题 ...
- vue编写一个登录页面,使用Tab栏实现“账号登录”和“二维码登录”这两种方式的切换
编写一个登录页面,使用Tab栏实现"账号登录"和"二维码登录"这两种方式的切换,并通过transition组件结合animate.css实现切换时的动画效果 1 ...
- Spring Cloud OAuth2 扩展登录方式:帐户密码登录、 手机验证码登录、 二维码扫码登录
本文扩展了spring security 的登录方式,增长手机验证码登录.二维码登录. 主要实现方式为使用自定义filter. AuthenticationProvider. AbstractAuth ...
- 2021-05-27 WMS系统中的二维码技术应用
目前,在仓库的日常管理过程中,传统的手工作业方式已无法适应现代物流发展的要求,特别是在货物信息查询,货物出入库管理.货物盘点核对等重要环节,由于信息采集不及时.标注不统一,容易造成仓库账实差,并且对仓 ...
最新文章
- 剑指offer:二叉树的下一个节点
- django框架-DRF工程之认证功能
- Java程序执行过程
- 内核调试相关变量说明
- ios 渐变透明背景_15张案例,告诉你PPT背景的处理套路
- 霸榜!Google发布语义分割新数据集!
- lvs基本概念、调度方法、ipvsadm命令及nat模型示例
- 物联网通信技术,那些你不知道的事
- Redis之内存分析
- python 多个装饰器的调用顺序
- Scratch 模拟病毒传染小程序
- 仓库装箱管理装箱发货,装箱扫描,装箱条码扫描系统成品装箱系统
- 城市数据大脑:小汽车儿堵成翔?NONONO!
- 六月申城如约而至,2021上海空气新风展邀您共襄行业盛举
- 彷徨 | office快捷键大全
- 抖音下载量超 Facebook;华为新款手机陷“绿屏”门;苹果又遭起诉 | 极客头条...
- 读后感:《产品经理修炼之道》读后感
- 云南怒江---地狱与天堂的边缘
- 将Opera默认搜索引擎改为百度
- H5C3常见知识点总结