python解决12306登录验证码的实现
在家无聊,线代和高数看不懂,整点事情干,就准备预定回学校的高铁票,于是就有了这个文章
准备工作1.pip安装chromediver,当然也可以手动解压(网上的教程好像没有提到pip,手动安装到C盘pycharm里面的Scripts就行了)chromedriver.storage.googleapis.com/index.html这是chromedriver文件官网,在chrome里面设置查看自己的版本,然后找对应的版本就完了
2.注册个超级鹰,http://www.chaojiying.com/contact.html,挺厉害的打码平台,微信公众号绑定一下账号给1000积分,足够干12306验证码了
开始实战讲解1.选择chrome打开12306然后切换到账号登录
默认是扫码登录
F12然后点击账号登录
3.复制xPath,/html/body/div[2]/div[2]/ul/li[2]/a
代码实现
from selenium.webdriver import Chromeweb = Chrome()web.get(’https://kyfw.12306.cn/otn/resources/login.html’)time.sleep(3)web.find_element_by_xpath(’/html/body/div[2]/div[2]/ul/li[2]/a’).click()
2.下载验证码(截屏也可以)然后发送给超级鹰
超级鹰官网有个官方文档,下载然后pychram打开,其实就很简单,然后把账号密码改成你自己的,
from chaojiying import Chaojiying_Client
验证码需要时间加载,所以要sleep(3)就够了,
3.拿到坐标然后模拟点击好像这个官方叫什么偏移量,挺高大上的,说白了就是建立一个坐标系,给个x,y然后点击就完了,默认左上方是原点
for pre_location in location_list:#切割出来x,y坐标location = pre_location.split(’,’)x = int(location[0])y = int(location[1])ActionChains(web).move_to_element_with_offset(img,x,y).click().perform()
4.登录以后有个滑动验证现在我还没有找到方法控制滑动速度,匀速运动,但是12306并没有因为这个验证失败
ActionChains(web).drag_and_drop_by_offset(button,340,0).perform()
button是那个滑块的Xpath,我记得好像是长度330,340肯定是够用了,那个0就是竖y的方向上的滑动
12306靠webdriver判断是不是爬虫刚开始12306图片和滑动验证通过以后一直说验证失败,百思不得其解,百度发现是因为这个
这是正常页面下的,也就是我改了以后的,加一个这个代码,欺骗一下
def trick_not_chromedriver(): option = Options() option.add_argument(’--disable-blink-features=AutomationControlled’) return option
这个要调用在前面,靠后一点就不行了
全部代码from selenium.webdriver import Chromeimport requests,timefrom hashlib import md5from selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.chrome.options import Optionsclass 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()#获取验证码def get_verify_img(web): web.find_element_by_xpath(’/html/body/div[2]/div[2]/ul/li[2]/a’).click() time.sleep(5) verify_img = web.find_element_by_xpath(’//*[@id='J-loginImg']’) return verify_img#识别验证码返回坐标def discern_verify_img(verify_img): chaojiying = Chaojiying_Client(’超级鹰账号’, ’密码’, ’软件ID’) responce = chaojiying.PostPic(verify_img.screenshot_as_png, 9004) pre_location = responce[’pic_str’] location_list = pre_location.split('|') # 把split写错了,卡了半天 # type_pre_location = type(pre_location) return location_list # return type_pre_location#拿到坐标模拟点击def click_and_enter(web,location_list,img): for pre_location in location_list:#切割出来x,y坐标location = pre_location.split(’,’)x = int(location[0])y = int(location[1])ActionChains(web).move_to_element_with_offset(img,x,y).click().perform()def enter(web): # input() web.find_element_by_xpath(’//*[@id='J-userName']’).send_keys(’账号’) web.find_element_by_xpath(’//*[@id='J-password']’).send_keys(’密码’) web.find_element_by_xpath(’//*[@id='J-login']’).click()#滑动验证def move_verify(web): button = web.find_element_by_xpath(’//*[@id='nc_1__scale_text']/span’) ActionChains(web).drag_and_drop_by_offset(button,340,0).perform()# 骗12306这不是chromedriverdef trick_not_chromedriver(): option = Options() option.add_argument(’--disable-blink-features=AutomationControlled’) return option#现在有一个疫情防控的确认按钮,点一下这个def yqfk(web): web.get(’https://kyfw.12306.cn/otn/leftTicket/init’) time.sleep(1) web.find_element_by_xpath(’//*[@id='qd_closeDefaultWarningWindowDialog_id']’).click()#进入查询界面,思路正则表达式,不可信def get_stick_text(web): web.get(’https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2021-04-16&leftTicketDTO.from_station=TNV&leftTicketDTO.to_station=CZF&purpose_codes=0X00’) response = web.find_element_by_xpath(’/html/body/pre’).text return (response)#父子节点一个一个找,显示余票if __name__ == ’__main__’: web = Chrome(options=trick_not_chromedriver()) web.get(’https://kyfw.12306.cn/otn/resources/login.html’) time.sleep(5) # click_and_enter(discern_verify_img(get_verify_img())) img = get_verify_img(web) click_and_enter(web,discern_verify_img(img),img) time.sleep(5) enter(web) time.sleep(5) move_verify(web) time.sleep(1) yqfk(web) time.sleep(2) get_verify_img(web)
已经可以登录的,结果就是这个界面
还有一个想法是余票检测,在搞了,应该快了
到此这篇关于python解决12306登录验证码的实现的文章就介绍到这了,更多相关python 12306登录验证码 内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章: