zoukankan      html  css  js  c++  java
  • Spider--实战--selenium_12306

    # login12306_02
    # 图像识别涉及到深度学习,这里直接将验证码识别任务发送到大佬的验证码解析地址,不过现在已经失效了,程序跑到这会报错。
    # 用户名和密码存储在本地工作目录中的 username_password_12306.txt文件中。
    
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import base64
    import re
    import time
    import requests
     
     
    class Login(object):
        def __init__(self, driver):
            self.driver = driver
    #         图片验证码坐标
            self.coordinate = [[-105, -20], [-35, -20], [40, -20], [110, -20], [-105, 50], [-35, 50], [40, 50], [110, 50]]
        
            
        def get_login_info(self):
            with open('username_password_12306.txt', 'r') as file_obj:
                lines = file_obj.readlines()
                username = lines[0].split()
                password = lines[1].split()
            return [username, password]
        
        
        def login(self):
            # 读取文件,获取账号和密码
            username = self.get_login_info()[0]
            password = self.get_login_info()[1]
            # 12306登陆页面
            login_url = "https://kyfw.12306.cn/otn/resources/login.html"
            # 设置浏览器长宽
            self.driver.set_window_size(1200, 900)
            # 打开登陆页面
            self.driver.get(login_url)
            # 找到账号登陆按钮  (有两种登陆方式,一种是扫码登录,一种是账号登录)。
            account = self.driver.find_element_by_class_name("login-hd-account")
            # 点击按钮
            account.click()
            # 找到用户名输入框
            userName = self.driver.find_element_by_id("J-userName")
            # 输入用户名
            userName.send_keys(username)
            # 找到密码输入框
            passWord = self.driver.find_element_by_id("J-password")
            # 输入密码
            passWord.send_keys(password)
     
        def getVerifyImage(self):
            try:
                # 找到图片验证码标签
                img_element = WebDriverWait(self.driver, 100).until(
                    EC.presence_of_element_located((By.ID, "J-loginImg"))
                )
     
            except Exception as e:
                print(u"验证码未加载,请检查您的网络设置!")
            # 获取图片验证码的src属性,就是图片base64加密后的数据
            base64_str = img_element.get_attribute("src").split(",")[-1]
            # base64解码得到图片的数据
            imgdata = base64.b64decode(base64_str)
            # 存入img.jpg
            with open('img.jpg', 'wb') as file:
                file.write(imgdata)
            self.img_element = img_element
     
        def getVerifyResult(self):
            '''解析返回结果,将坐标存放到列表里'''
            # 12306验证码识别网址
            url = "http://littlebigluo.qicp.net:47720/"   # 目前,这个网址被封了,无法使用
            # 发送post请求把图片数据带上
            response = requests.request("POST", url, data={"type": "1"}, files={'pic_xxfile': open('img.jpg', 'rb')})
            result = []
            print(response.text)
            # 返回识别结果
            for i in re.findall("<B>(.*)</B>", response.text)[0].split(" "):
                result.append(int(i) - 1)
            self.result = result
            print(result)
     
        def moveAndClick(self):
            # 根据目标验证码的位置,点击相应的图片。
            try:
                # 创建鼠标对象
                Action = ActionChains(self.driver)
                for i in self.result:
                    # 根据获取的结果取坐标选择图片并点击
                    Action.move_to_element(self.img_element).move_by_offset(self.coordinate[i][0],
                                                                            self.coordinate[i][1]).click()
                Action.perform()
            except Exception as e:
                print(e)
     
        def submit(self):
            # 点击登陆按钮
            self.driver.find_element_by_id("J-login").click()
     
     
    if __name__ == '__main__':
        driver=webdriver.Firefox()
        login = Login(driver)
        login.login()
        time.sleep(3)
        login.getVerifyImage()
        time.sleep(1)
        login.getVerifyResult()
        time.sleep(1)
        login.moveAndClick()
        time.sleep(1)
        login.submit()
        time.sleep(10)
    #     driver.close()
    #     driver.quit()
    
    

    参考资料:
    https://www.52pojie.cn/thread-1048861-1-1.html

  • 相关阅读:
    线程池和进程池
    初识数据库
    线程q
    event事件
    死锁和递归锁
    信号量
    PythonStudy——线程中的几种消息队列
    PythonStudy——GIL Global Interpreter Lock 全局解释器锁
    PythonStudy——异步回调
    PythonStudy——日志模块 logging
  • 原文地址:https://www.cnblogs.com/Collin-pxy/p/13038634.html
Copyright © 2011-2022 走看看