selenium+超级鹰模拟登录12306购票网

selenium+超级鹰模拟登录12306购票网

超级鹰为专业的人工打码平台,收费很廉价,1元钱可以玩好久。有兴趣的可以试一试。12306这种坑爹的验证码貌似只有这一种方法可以破解。
本文章仅限模拟登录,查询及抢票请关注后续文章。

selenium+超级鹰模拟登录12306购票网_第1张图片

超级鹰模块代码:

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


def my_chaojiying(img):
    username = '你的超级鹰账号'
    password = '你的超级鹰密码'
    appid = '你的超级鹰appid'
    chaojiying = Chaojiying_Client(username, password, appid)  # 用户中心>>软件ID 生成一个替换 96001
    im = open(img, 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    return chaojiying.PostPic(im, 9004)  # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()

模拟登录

from selenium import webdriver
from My_Spider.API.Api_tools import Api_Url
import time
from PIL import Image
from My_Spider.API.chaojiying import my_chaojiying
from My_Spider.API.Api_tools import ApiTools
from selenium.webdriver.support.ui import WebDriverWait # 显示等待
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC # 条件等待
from selenium.webdriver import ActionChains
import requests
import json
from fake_useragent import UserAgent

class UserPass(object):
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.station_data = ''

    def login(self):
        # 代理ip
        proxy = '60.188.241.255:3000'
        # 设置代理ip
        chrome_option = webdriver.ChromeOptions()
        chrome_option.add_argument('--proxy-server = %s' % proxy)

        driver = webdriver.Chrome(chrome_options=chrome_option)

        # 请求
        driver.get(Api_Url.login_url1)
        driver.find_element_by_xpath('//*[@id="username"]').send_keys(self.username)
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(self.password)
        time.sleep(2)
        # 获取图片
        img = driver.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
        location = img.location
        print(location)
        size = img.size
        print(size)
        # 左上右下
        img_full_location = (location['x']*1.25,location['y']*1.25,(location['x']+size['width'])*1.25,(location['y']+size['height'])*1.25)
        print(img_full_location)

        driver.save_screenshot('screen.png')
        i = Image.open('screen.png')
        cut_img = i.crop(img_full_location)
        cut_img.save('验证码.png')

        code = my_chaojiying('验证码.png')
        print(code)

        pic_id = code['pic_id']
        if code['err_str'] == 'OK' and code['err_str'] != '无可用题分':
            result = code.get('pic_str').split('|')
            points = [[int(number) for number in numbers.split(',')] for numbers in result]
            for point in points:
                print(point)
                element = WebDriverWait(driver,10).until(
                    EC.presence_of_element_located((By.CLASS_NAME,"touclick-image"))
                )
                # 模拟鼠标点击
                ActionChains(driver).move_to_element_with_offset(element,point[0]/1.25,point[1]/1.25).click().perform()
                time.sleep(1)
            time.sleep(1)
            driver.find_element_by_xpath('//*[@id="loginSub"]').click()
            time.sleep(5)
            if driver.current_url not in [Api_Url.login_url1,Api_Url.login_url2]:
                print('登录成功!')
                el = WebDriverWait(driver,20).until(
                    EC.presence_of_element_located((By.CSS_SELECTOR,"[class='btn btn-primary ok']"))
                )
                el.click()

                time.sleep(10)


            else:
                print('登录失败请重试!')
                time.sleep(10)
        else:
        	#这段代码用来异常时向超级鹰报告错误防止扣钱。pic_id为超级鹰返回的操作代码
            ApiTools.err_post('你的超级鹰账号', '你的超级鹰密码', pic_id, '你的超级鹰appid')

if __name__ == '__main__':
    username = '你的12305账号'
    password = '你的12305密码'
    user = UserPass(username, password)
    user.login()

你可能感兴趣的:(selenium+超级鹰模拟登录12306购票网)