zoukankan      html  css  js  c++  java
  • 12306自动登录

      1 from hashlib import md5
      2 from selenium import webdriver
      3 import time
      4 import requests
      5 from selenium.webdriver import ActionChains
      6 from PIL import Image
      7 
      8 
      9 class Chaojiying_Client(object):
     10 
     11     def __init__(self, username, password, soft_id):
     12         self.username = username
     13         password = password.encode('utf8')
     14         self.password = md5(password).hexdigest()
     15         self.soft_id = soft_id
     16         self.base_params = {
     17             'user': self.username,
     18             'pass2': self.password,
     19             'softid': self.soft_id,
     20         }
     21         self.headers = {
     22             'Connection': 'Keep-Alive',
     23             'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
     24         }
     25 
     26     def PostPic(self, im, codetype):
     27         """
     28         im: 图片字节
     29         codetype: 题目类型 参考 http://www.chaojiying.com/price.html
     30         """
     31         params = {
     32             'codetype': codetype,
     33         }
     34         params.update(self.base_params)
     35         files = {'userfile': ('ccc.jpg', im)}
     36         r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files,
     37                           headers=self.headers)
     38         return r.json()
     39 
     40     def ReportError(self, im_id):
     41         """
     42         im_id:报错题目的图片ID
     43 
     44 
     45         """
     46         params = {
     47             'id': im_id,
     48         }
     49         params.update(self.base_params)
     50         r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
     51         return r.json()
     52 
     53 
     54 bro = webdriver.Chrome()
     55 bro.get('https://kyfw.12306.cn/otn/login/init')
     56 
     57 time.sleep(3)
     58 # 定位到验证码图片对应的img标签
     59 code_img_ele = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
     60 
     61 time.sleep(3)
     62 # 上一步定位到验证码图片标签的位置坐标(左上角的坐标)
     63 location = code_img_ele.location  # x,y
     64 # size返回的就是验证按摩图片的尺寸
     65 size = code_img_ele.size  # width and height
     66 # rangle对应的就是验证码图片的裁剪区域
     67 rangle = (
     68     int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
     69 
     70 # save_screenshot将当前浏览器打开的页面进行整体截图
     71 bro.save_screenshot('aa.png')
     72 # 进行指定区域的截图
     73 i = Image.open('./aa.png')
     74 # 验证码图片的名称
     75 code_img_name = 'code.png'
     76 # 根据指定区域实现裁剪
     77 frame = i.crop(rangle)
     78 frame.save(code_img_name)
     79 
     80 # 将验证码图片提交给超级鹰进行识别
     81 chaojiying = Chaojiying_Client('32841094', '32841094', '89937')  # 用户中心>>软件ID 生成一个替换 96001
     82 im = open('./code.png', 'rb').read()
     83 result = chaojiying.PostPic(im, 9004)['pic_str']
     84 # "x1,y1|x2,y2"     "x,y"
     85 
     86 # [['x1','y1'],['x2','y2']]
     87 # [['x','y']]
     88 
     89 all_list = []
     90 if '|' in result:
     91     list_1 = result.split('|')
     92     count_1 = len(list_1)
     93     for i in range(count_1):
     94         xy_list = []
     95         x = int(list_1[i].split(',')[0])
     96         y = int(list_1[i].split(',')[1])
     97         xy_list.append(x)
     98         xy_list.append(y)
     99         all_list.append(xy_list)
    100 else:
    101     x = int(result.split(',')[0])
    102     y = int(result.split(',')[1])
    103     xy_list = []
    104     xy_list.append(x)
    105     xy_list.append(y)
    106     all_list.append(xy_list)
    107 print(all_list)
    108 
    109 code_img = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
    110 
    111 action = ActionChains(bro)
    112 
    113 for l in all_list:
    114     x = l[0]
    115     y = l[1]
    116     ActionChains(bro).move_to_element_with_offset(code_img, x, y).click().perform()
    117 
    118 bro.find_element_by_id('username').send_keys('xxxxxx')
    119 time.sleep(2)
    120 bro.find_element_by_id('password').send_keys('xxxxx')
    121 time.sleep(2)
    122 bro.find_element_by_id('loginSub').click()
    123 time.sleep(10)
    124 bro.quit()
  • 相关阅读:
    在Windows Mobile 中如何改变Datagrid中字的颜色?
    使用FileSystemWatcher监视文件更改
    MVVM体验记之DataGrid绑定
    实现IDisposable以实现更优雅的代码
    适当使用enum做数据字典
    web.config customErrors无法处理的is not a valid virtual path
    Windows下使用skipfish
    MVVM打造无限级TreeView
    System.Drawing.Image在Save之后Type变了
    Windows下使用RatProxy
  • 原文地址:https://www.cnblogs.com/wangtaobiu/p/11044524.html
Copyright © 2011-2022 走看看