• 爬虫 12306模拟登录(运用超级鹰 selenium)


    调用了超级鹰(两个文件  ,从超级鹰导入文件)

    #!/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()

    注意电脑分辨率调为100%(这样图片位置才能正好 )(或者里面的x,y 对应等比例缩放)

    from PIL import Image
    from selenium import webdriver
    from chaojiying import Chaojiying_Client
    from selenium.webdriver import ActionChains
    from time import sleep
    #返回验证码对应的数据
    def getCode(imgPath,imgType):
        chaojiying = Chaojiying_Client('xlhchaojiying', 'xlhchaojiying', '900927')  # 用户中心>>软件ID 生成一个替换 96001
        im = open(imgPath, 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
        return chaojiying.PostPic(im,imgType)['pic_str']
    
    
    bro = webdriver.Chrome(executable_path='chromedriver.exe')
    url = 'https://kyfw.12306.cn/otn/login/init'
    bro.get(url)
    sleep(3)
    
    #验证码进行截取且识别
    bro.save_screenshot('main.png')
    
    #确定截取的范围
    img_ele= bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
    #location表示的是验证码图片左上角的坐标
    location = img_ele.location
    print('location:',location)
    #size返回的是图片的长和宽
    size = img_ele.size
    print('size:',size)
    #rangle就是制定好的截取范围
    rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height']))
    
    #根据截取范围进行截图
    i = Image.open('main.png')
    #code_img_name截取下验证码图片的名称
    code_img_name = 'code.png'
    frame = i.crop(rangle)
    frame.save(code_img_name)
    
    #记录:code.png就是验证码图片  main.png就是当前登陆页面对应的图片
    result = getCode('code.png',9004)
    print(result)
    #返回的单组坐标数据:242,84
    #返回的多组坐标数据:242,84|12,54|44,66
    #[[1,2],[3,4],[5,6]]
    #[[1,2]]
    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])
        y = int(result.split(',')[1])
        xy_list = []
        xy_list.append(x)
        xy_list.append(y)
        all_list.append(xy_list)
    
    print(all_list)
    
    #基于selenium根据all_list进行定点的点击操作
    for l in all_list:
        x = l[0]
        y = l[1]
        ActionChains(bro).move_to_element_with_offset(img_ele,x,y).click().perform()
        sleep(0.5)
    
    bro.find_element_by_id('username').send_keys('yonghuming')
    sleep(2)
    bro.find_element_by_id('password').send_keys('changmima')
    sleep(2)
    bro.find_element_by_id('loginSub').click()
    sleep(10)
    bro.quit()
  • 相关阅读:
    第十六章 Centos7下Docker安装GitLab
    第二十六章 Kubeadm方式搭建kubernetes高可用集群
    第二十五章 Kubernetes部署Promethus集群资源监控
    小程序遮罩层效果实现
    docker停止所有,删除容器命令
    sp欢迎页支持
    c#学习
    静态资源访问前缀
    webjar使用
    springboot静态资源配置原理
  • 原文地址:https://www.cnblogs.com/XLHIT/p/11317884.html
Copyright © 2020-2023  润新知