selenium 12306模拟登陆

代码应用场景 :基于第三方打码网站模拟登陆12306

  验证码识别

    基于第三方平台超级鹰识别

    超级鹰官网:http://www.chaojiying.com/user/

  超级鹰使用流程:

    注册 登陆(用户中心)充值

    创建一个软件:软件ID->生成一个软件ID(901977)

    下载实例代码->开发文档->python

1. 重新封装在打码平台下载到的python代码

#!/usr/bin/env python
# coding:utf-8

import requests
from hashlib import md5

class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode(‘utf8‘)
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.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()

2.登陆12306代码逻辑

from selenium import webdriver
from selenium.webdriver import ActionChains
from CJY import Chaojiying_Client
from time import sleep
from PIL import Image

#pip install Pillow

def get_code_text(imgPath,imgType):
    chaojiying = Chaojiying_Client(‘超级鹰账号‘, ‘超级鹰密码‘, ‘创建的软件ID‘)  # 用户中心>>软件ID 生成一个替换 96001
    im = open(imgPath, ‘rb‘).read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    return chaojiying.PostPic(im, imgType)

bro = webdriver.Chrome(executable_path=‘./chromedriver.exe‘)
bro.get(‘https://kyfw.12306.cn/otn/login/init‘)
sleep(2)
userName = bro.find_element_by_id(‘username‘)
userName.send_keys(‘12306账号‘)
sleep(2)
passWord = bro.find_element_by_id(‘password‘)
passWord.send_keys(‘12306密码‘)

# bro.set_window_size(800,600)
bro.set_window_size(1920,1080)

#验证码识别,实现点击操作
bro.save_screenshot(‘./main.png‘) #当前页面所对应的一整张图片

#验证码图片的标签
code_img_tag = bro.find_element_by_xpath(‘//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img‘)

#验证码图片左下角坐标
location = code_img_tag.location #location返回的是位置
size = code_img_tag.size
# 裁剪区域
rangle = (int(location[‘x‘]),int(location[‘y‘]),int(location[‘x‘])+size[‘width‘],int(location[‘y‘]+size[‘height‘]))

i = Image.open(‘./main.png‘)
frame = i.crop(rangle)
frame.save(‘./code.png‘)

img_text = get_code_text(‘./code.png‘,9004)
img_text = img_text[‘pic_str‘]
print(img_text,type(img_text))

all_list = []
if ‘|‘ in img_text:
    list_1 = img_text.split(‘|‘)
    count_1 = len(list_1)
    for i in range(count_1):
        xy_list = []
        x = int(list_1[i].split(‘,‘)[0])
        y = int(list_1[i].split(‘,‘)[1])
        xy_list.append(x)
        xy_list.append(y)
        print(xy_list)
        all_list.append(xy_list)

else:
    x = int(img_text.split(‘,‘)[0])
    y = int(img_text.split(‘,‘)[1])
    xy_list = []
    xy_list.append(x)
    xy_list.append(y)
    all_list.append(xy_list)

print(all_list)
for p in all_list:
    x = p[0]
    y = p[1]
    # x ,y 就是即将要点击的坐标
    ActionChains(bro).move_to_element_with_offset(code_img_tag,x,y).click().perform()
    sleep(1)

login_btn = bro.find_element_by_xpath(‘//*[@id="loginSub"]‘)
login_btn.click()

sleep(5)
bro.quit()

ps: 如果不成功,最好是把自己电脑文字分辨率调整成100%

原文地址:https://www.cnblogs.com/guniang/p/11727824.html

时间: 2024-07-31 12:29:56

selenium 12306模拟登陆的相关文章

12306模拟登陆-超级鹰

12306模拟登陆-超级鹰 什么是超级鹰? 就是和云打码类似的验证码识别网站,但是他能识别更复杂的图片验证码 模拟登陆12306 主要思路 1.首先登陆到12306界面 2.点击账号密码登录,到账号密码登录模块 3.截取整张界面的图片保存到本地,并获取验证码部分的坐标 4.在界面截图的基础上,根据获取的坐标截取验证码的图片保存到本地 5.把图片交给超级鹰,返回结果 6.把结果解析成想要点击的坐标 7.输入账号密码,执行验证码点击,点击登录 8.ok,但是有几率无法识别验证码 from selen

利用Python与selenium自动化模拟登陆12306官网!

近年来,12306的反爬越来越来严重,从一年前的 获取tk参数后到现在增加了 JS.CSS等加密方式! 目前大部分人利用的登陆方式都是利用selenium ,此文也不例外. 环境:        Windows python 3.6.5 模块:      selenium pyautogui      time 第一步: 实例化一款浏览器,并进入到12306官网 driver = webdriver.Chrome() driver.get('https://kyfw.12306.cn/otn/r

利用selenium+chrome模拟登陆合工大信息门户并进行自动填写测评

最近学校要填写对于老师的评教,不填写的就无法进行下周的选课∑^)/ 我这么懒,自然不想一个一个点进去填写,想到最近在学爬虫,干脆写一个爬虫帮我弄算了 ╭~~~╮ (o~.~o) 首先打开我们学校的信息门户:http://my.hfut.edu.cn/(仅限校园网) 可以看到我们学校的北门\(0^◇^0)/ 可以看出来,用户名和密码还是很好填的,这个验证码就很麻烦了...... 我选择的方法是,直接保存屏幕截图,然后利用定位元素位置定量裁剪图片,然后利用图像识别识别图中验证码, 当然,直接选图片自

Python模拟登陆万能法-微博|知乎

Python模拟登陆让不少人伤透脑筋,今天奉上一种万能登陆方法.你无须精通HTML,甚至也无须精通Python,但却能让你成功的进行模拟登陆.本文讲的是登陆所有网站的一种方法,并不局限于微博与知乎,仅用其作为例子来讲解. 用到的库有"selenium"和"requests".通过selenium进行模拟登陆,然后将Cookies传入requests,最终用requests进行网站的抓取.优点就是不但规避了"selenium"其本身抓取速度慢的问题

Python模拟登陆万能法

此文转自:https://zhuanlan.zhihu.com/p/28587931   转录只是为了方便学习,感谢他的分享 Python模拟登陆让不少人伤透脑筋,今天奉上一种万能登陆方法.你无须精通HTML,甚至也无须精通Python,但却能让你成功的进行模拟登陆.本文讲的是登陆所有网站的一种方法,并不局限于微博与知乎,仅用其作为例子来讲解. 用到的库有"selenium"和"requests".通过selenium进行模拟登陆,然后将Cookies传入reque

Python爬虫(二十二)_selenium案例:模拟登陆豆瓣

本篇博客主要用于介绍如何使用selenium+phantomJS模拟登陆豆瓣,没有考虑验证码的问题,更多内容,请参考:Python学习指南 #-*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.common.keys import Keys import time #如果获取页面时获取不到文本内容,加入下面参数 driver = webdriver.PhantomJS(service_args=[

selenium 模拟登陆豆瓣,爬去武林外传的短评

selenium 模拟登陆豆瓣,爬去武林外传的短评: 在最开始写爬虫的时候,抓取豆瓣评论,我们从F12里面是可以直接发现接口的,但是最近豆瓣更新,数据是JS异步加载的,所以没有找到合适的方法爬去,于是采用了selenium来模拟浏览器爬取. 豆瓣登陆也是改了样式,我们可以发现登陆页面是在另一个frame里面 所以代码如下: # -*- coding:utf-8 -*- # 导包 import time from selenium import webdriver from selenium.we

selenium自动化测试工具模拟登陆爬取当当网top500畅销书单

selenium自动化测试工具可谓是爬虫的利器,基本动态加载的网页都能抓取,当然随着大型网站的更新,也出现针对selenium的反爬,有些网站可以识别你是否用的是selenium访问,然后对你加以限制. 当当网目前还没有对这方面加以限制,所以今天就用这个练习熟悉一下selenium操作,我们可以试一下爬取一下当当网top500的畅销书单的相关信息,页面如下: 虽然这个页面不用登录就可以进来,但是我们可以随便试一下模拟登陆,直接在这个页面上面点击登录进入登录界面,然后会弹出一下窗口, 这是百分百会

模拟登陆 12306网站

目录 模拟登陆 12306网站 准备 需求分析 实现代码 原文地址:https://www.cnblogs.com/guokaifeng/p/11695389.html