12306 selenium 模拟登录

# 下面是12306 实现的模拟登陆

# 解码 应用超级鹰,注册用户,左侧栏软件ID进去,开启一个新软件,拿到软件ID

# 下面测试都在jupyter里面实现

# 超级鹰类  cell
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):

        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)
        print(r.json()) #{'err_no': 0, 'err_str': 'OK', 'pic_id': '9067216592371000003', 'pic_str': '24,62|40,146|112,141', 'md5': 'c4ee4d4fb269521c47de228d5c6d6d3e'}
        return r.json()

    def ReportError(self, im_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()

  # 下面是12306 页面的处理

from selenium import webdriver
import time
import requests
from lxml import etree
from urllib import request
from selenium.webdriver.common.action_chains import ActionChains
from PIL import Image
bro = webdriver.Chrome(executable_path='./chromedriver.exe')
bro.get('https://kyfw.12306.cn/otn/login/init')
# page_text = bro.page_source
# tree = etree.HTML(page_text)
# code_img_src = tree.xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img/@src')[0]
# print(code_img_src)
# request.urlretrieve(url=code_img_src,filename='./code.jpg')
time.sleep(3)

code_img_ele = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')  #找见验证码区域
time.sleep(3)
location = code_img_ele.location  # x,y
print('--',location)  # -- {'x': 274, 'y': 274}
size = code_img_ele.size 
print('++',location)  # ++ {'x': 274, 'y': 274}
rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #验证码图裁剪区域 矩形
print('**',rangle)  #矩形 四点 像素值
bro.save_screenshot('aa.png')  # 快照  整张页面截图
# 指定区域的截图
i = Image.open('./aa.png') #打开图片 code_img_name = 'code.png' #最终验证码裁剪出来的保存名称 frame = i.crop(rangle)   #裁剪 frame.save(code_img_name) #裁剪成功的图片保存 # 将验证码图片提交给 超级鹰 处理 先实例化 chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '软件ID')#超级鹰用户中心>>软件ID 生成一个替换上 im = open('./code.png','rb').read() #读取验证码图片 result = chaojiying.PostPic(im, 9004)['pic_str'] # 返回坐标 # 'x1,y1|x2,y2' 'x,y' result值 转换成[['x','y']] [['x1','y1'],['x2','y2']] all_list = [] if '|' in result: list_1 = result.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) all_list.append(xy_list) else: x = int(result.split(',')[0]) print(x) y = int(result.split(',')[1]) xy_list = [] xy_list.append(x) xy_list.append(y) all_list.append(xy_list) print(all_list) # [[24, 62], [40, 146], [112, 141]] 坐标位置 #code_img = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img') action = ActionChains(bro) #动作链 实例化一个对象 for l in all_list: x = l[0] y = l[1] ActionChains(bro).move_to_element_with_offset(code_img_ele,x,y).click().perform() bro.find_element_by_id('username').send_keys('') #12306账号 time.sleep(2) bro.find_element_by_id('password').send_keys('') #12306密码 time.sleep(2) bro.find_element_by_id('loginSub').click() time.sleep(10) bro.quit() # 退出

 

转载于:https://www.cnblogs.com/zhangchen-sx/p/10826837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么学爬虫?】        1、爬虫入手容易,但是深入较难,如何写出高效率的爬虫,如何写出灵活性高可扩展的爬虫都是一项技术活。另外在爬虫过程中,经常容易遇到被反爬虫,比如字体反爬、IP识别、验证等,如何层层攻克难点拿到想要的数据,这门课程,你都能学到!        2、如果是作为一个其他行业的开发者,比如app开发,web开发,学习爬虫能让你加强对技术的认知,能够开发出更加安全的软件和网站 【课程设计】 一个完整的爬虫程序,无论大小,总体来说可以分成三个步骤,分别是:网络请求:模拟浏览器的行为从网上抓取数据。数据解析:将请求下来的数据进行过滤,提取我们想要的数据。数据存储:将提取到的数据存储到硬盘或者内存中。比如用mysql数据库或者redis等。那么本课程也是按照这几个步骤循序渐进的进行讲解,带领学生完整的掌握每个步骤的技术。另外,因为爬虫的多样性,在爬取的过程中可能会发生被反爬、效率低下等。因此我们又增加了两个章节用来提高爬虫程序的灵活性,分别是:爬虫进阶:包括IP代理,多线程爬虫,图形验证识别、JS加密解密、动态网页爬虫、字体反爬识别等。Scrapy和分布式爬虫:Scrapy框架、Scrapy-redis组件、分布式爬虫等。通过爬虫进阶的知识点我们能应付大量的反爬网站,而Scrapy框架作为一个专业的爬虫框架,使用他可以快速提高我们编写爬虫程序的效率和速度。另外如果一台机器不能满足你的需求,我们可以用分布式爬虫让多台机器帮助你快速爬取数据。 从基础爬虫到商业化应用爬虫,本套课程满足您的所有需求!【课程服务】 专属付费社群+定期答疑

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值