zoukankan      html  css  js  c++  java
  • python爬虫之selenium+打码平台识别验证码

    1、常用的打码平台:超级鹰、打码兔等

    2、打码平台在识别图形验证码和点触验证码上比较好用

      (1)12306点触验证码

     1 from selenium import webdriver
     2 from selenium.webdriver.support import expected_conditions as EC
     3 from selenium.webdriver.support.wait import WebDriverWait
     4 from selenium.webdriver.common.by import By
     5 from selenium.webdriver.common.action_chains import ActionChains
     6 from chaojiying import Chaojiying_Client
     7 from account import USERNAME12306, PASSWORD12306, USERNAMECJY, PASSWORDCJY
     8 import time
     9 
    10 
    11 class TrainLogin(object):
    12     def __init__(self, username, password):
    13         self.username = username
    14         self.password = password
    15         self.index_url = "https://www.12306.cn/index/"
    16         self.login_url = "https://kyfw.12306.cn/otn/resources/login.html"
    17         self.code_path = r"E:vscodePython超级鹰验证码识别chaojiying_Python12306.png"
    18         self.chrome = webdriver.Chrome(r"D:chromedriver_win32chromedriver.exe")
    19         self.chaojiying = Chaojiying_Client(USERNAMECJY, PASSWORDCJY, "903176")
    20 
    21     def login(self):
    22         self.pre_set()
    23         self.come_login_url()
    24         self.process_username_password()
    25         while True:
    26             self.process_captchar()
    27             if self.success_login():
    28                 print("成功登陆!!!")
    29                 break
    30         time.sleep(5)
    31         self.chrome.quit()
    32 
    33     def pre_set(self):
    34         self.chrome.implicitly_wait(1)
    35         self.chrome.maximize_window()
    36 
    37     def come_login_url(self):
    38         self.chrome.get(self.login_url)
    39         WebDriverWait(self.chrome, 5).until(EC.presence_of_element_located((By.LINK_TEXT, "账号登录")))
    40         self.chrome.find_element_by_link_text("账号登录").click()
    41 
    42     def process_username_password(self):
    43         self.chrome.find_element_by_id("J-userName").send_keys(self.username)
    44         time.sleep(1)
    45         self.chrome.find_element_by_id("J-password").send_keys(self.password)
    46 
    47     def process_captchar(self):
    48         self.chrome.find_element_by_class_name("login-pwd-code").screenshot(self.code_path)
    49         with open(self.code_path, "rb") as fp:
    50             img = fp.read()
    51         res = self.chaojiying.PostPic(img, 9004)["pic_str"]
    52         pos_ls = self.get_postion_ls(res)
    53         action_chains = ActionChains(self.chrome)
    54         for pos in pos_ls:
    55             action_chains.move_to_element_with_offset(self.chrome.find_element_by_class_name("login-pwd-code"), 
    56             pos[0], pos[1]).click().release().perform()
    57             time.sleep(0.5)
    58             action_chains = ActionChains(self.chrome)
    59 
    60     def success_login(self):
    61         self.chrome.find_element_by_id("J-login").click()
    62         try:
    63             WebDriverWait(self.chrome, 5).until(EC.text_to_be_present_in_element((By.CLASS_NAME, "welcome-name"), "XXX"))
    64         except:
    65             return False
    66         else:
    67             return True
    68 
    69     def get_postion_ls(self, pos_str):
    70         pos_ls = []
    71         for pa in pos_str.split("|"):
    72             pos = []
    73             for pb in pa.split(","):
    74                 pos.append(int(pb))
    75             pos = tuple(pos)
    76 
    77             pos_ls.append(pos)
    78         return pos_ls
    79 
    80 
    81 if __name__ == "__main__":
    82     loginer = TrainLogin(USERNAME12306, PASSWORD12306)
    83     loginer.login()
    View Code

      (2)哔哩哔哩点触验证码

     1 from selenium import webdriver
     2 from selenium.webdriver.support import expected_conditions as EC
     3 from selenium.webdriver.support.wait import WebDriverWait
     4 from selenium.webdriver.common.by import By
     5 from selenium.webdriver.common.action_chains import ActionChains
     6 from chaojiying import Chaojiying_Client
     7 from account import USERNAMEBL, PASSWORDBL, USERNAMECJY, PASSWORDCJY
     8 import time
     9 
    10 class BiLiLogin(object):
    11     def __init__(self, username, password):
    12         self.username = username
    13         self.password = password
    14         self.chrome = webdriver.Chrome(r"D:chromedriver_win32chromedriver.exe")
    15         self.code_path = r"E:vscodePython超级鹰验证码识别chaojiying_Pythonili.png"
    16         self.login_url = "https://passport.bilibili.com/login"
    17         self.chaojiying = Chaojiying_Client(USERNAMECJY, PASSWORDCJY, "903176")
    18 
    19 
    20     def login(self):
    21         self.pre_set()
    22         self.come_login_win()
    23         self.process_username_password()
    24         while True:
    25             self.process_captchar()
    26             if self.is_success():
    27                 print("登录成功!!!")
    28                 break
    29         print(self.chrome.get_cookies())
    30         # 延迟十秒退出
    31         time.sleep(10)
    32         self.chrome.quit()
    33 
    34 
    35     def pre_set(self):
    36         self.chrome.implicitly_wait(1)
    37         self.chrome.maximize_window()
    38     
    39     def come_login_win(self):
    40         self.chrome.get(self.login_url)
    41 
    42     def process_username_password(self):
    43         self.chrome.find_element_by_id("login-username").send_keys(self.username)
    44         self.chrome.find_element_by_id("login-passwd").send_keys(self.password)
    45         self.chrome.find_element_by_class_name("btn-login").click()
    46 
    47     def process_captchar(self):
    48         WebDriverWait(self.chrome, 5).until(EC.presence_of_element_located((By.CLASS_NAME, "geetest_widget")))
    49         time.sleep(5)
    50         self.chrome.find_element_by_class_name("geetest_widget").screenshot(self.code_path)
    51         with open(self.code_path, "rb") as fp:
    52             img = fp.read()
    53         pos_str = self.chaojiying.PostPic(img, "9004")["pic_str"]
    54         pos_ls = self.get_postion_ls(pos_str)
    55         for pos in pos_ls:
    56             action_chain = ActionChains(self.chrome)
    57             action_chain.move_to_element_with_offset(self.chrome.find_element_by_class_name("geetest_widget"), pos[0], pos[1]).click().release().perform()
    58             time.sleep(0.5)
    59         self.chrome.find_element_by_class_name("geetest_commit").click()
    60 
    61     def is_success(self):
    62         try:
    63             WebDriverWait(self.chrome, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "nickname")))
    64         except:
    65             return False
    66         else:
    67             return True
    68         
    69 
    70     def get_postion_ls(self, pos_str):
    71         pos_ls = []
    72         for pa in pos_str.split("|"):
    73             pos = []
    74             for pb in pa.split(","):
    75                 pos.append(int(pb))
    76             pos = tuple(pos)
    77 
    78             pos_ls.append(pos)
    79         return pos_ls
    80         
    81 
    82 if __name__ == "__main__":
    83     loginer = BiLiLogin(USERNAMEBL, PASSWORDBL)
    84     loginer.login()
    View Code
  • 相关阅读:
    检测主机存活
    centos7 安装python3
    zbb20180103 git 配置用户名和邮箱
    zbb20171215 git 版本回退
    zbb20171215 bootstrap model 模态框
    zbb20171215 MyBatis报错: Parameter '*' not found. Available parameters are [1, 0, param1, param2]
    zbb20171215 thymeleaf th:attr
    zbb20171215ztree ztree出现$.fn.zTree is undefined错误的解决办法
    zbb20171215 bootstrap 日期控件.rar
    zbb20171215 oracle replace 替换单引号
  • 原文地址:https://www.cnblogs.com/loveprogramme/p/12209296.html
Copyright © 2011-2022 走看看