婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > selenium+超級鷹實現模擬登錄12306

selenium+超級鷹實現模擬登錄12306

熱門標簽:萬利達百貨商場地圖標注 河南虛擬外呼系統公司 外呼電信系統 熱門電銷機器人 惠州龍門400電話要怎么申請 智能機器人電銷神器 okcc外呼系統怎么調速度 上海企業外呼系統 電話機器人哪里有賣

最近迷上了用selenium去登陸各大網站,別說selenium真挺好用,可以輕松搞定ajax動態加載的網頁,不用很費勁的去抓包查找。咳咳…跑題了,回歸正題。

這次用selenium去登錄12306網站,聽說比較困難。我就去試了試,發現它的驗證碼實在是那啥…就是這樣的。聽頭疼的。


我來說說主要的代碼編寫吧。

過程:

用我們的開發者工具定位到輸入賬號和密碼的窗口,找到并send_keys

driver.find_element_by_id('username').send_keys('用戶名')
time.sleep(0.5)
driver.find_element_by_id('password').send_keys('密碼')

然后復雜的過程就來了。我們想要得到驗證碼的圖片。但是頭疼的是,圖片是再變化的。我們請求一次,就變化一次,不像其他普通網站一樣不會變化,直接保存圖片就行了。但是這是12306誒,哪這么輕松。想了想,我決定把整張頁面截屏保存下來,然后對驗證碼區域裁剪下來,就可以保證一致了。

# 將頁面進行截圖并保存
driver.save_screenshot('12306登錄頁面截圖.png')

# 確定驗證碼左上角和右下角的坐標
code_img = driver.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
location = code_img.location # 確定驗證碼圖片左上角的坐標
print('location:', location)
size = code_img.size # 確定驗證碼圖片的長和寬
print('size:', size)
rangle = (int(location['x']), int(location['y']), int(location['x']) + int(size['width']),
     int(location['y']) + int(size['height']))
print('rangle:', rangle)
i = Image.open('12306頁面截圖.png')
# 對指定區域裁剪
code_pic = i.crop(rangle)
file_name = 'code_pic.png'
code_pic.save(file_name)
time.sleep(2)
print('驗證碼圖片保存成功!!')

我們識別驗證碼用的是超級鷹,具體如何使用可以去查一查。驗證碼有可能需要我們點擊多個,所以通過打碼平臺會得到多個坐標,就比如這種。有兩個日歷,需要點擊兩次,通過超級鷹就會得到兩個坐標。如下圖。我們發現有兩個坐標會有一個“|”,有三個坐標就有兩個“|”,所以我們就把他們split下,讓每個坐標嵌套再一個列表里。此過程代碼如下:

# 識別驗證坐標
chaojiying = Chaojiying_Client('用戶賬號', '密碼', '開發者賬號') # 用戶中心>>軟件ID 生成一個替換 96001
im = open('code_pic.png', 'rb').read() # 本地圖片文件路徑 來替換 a.jpg 有時WIN系統須要//
result = chaojiying.PostPic(im, 9004)['pic_str'] # 1902 驗證碼類型 官方網站>>價格體系 3.4+版 print 后要加()

all_list = [] # 存儲被點擊的坐標
if '|' in result:
  list1 = result.split('|')
  xy_list = []
  count1 = len(list1)
  for i in list1:
    x = int(list1[i].split(',')[0])
    xy_list.append(x)
    y = int(list1[i].split(',')[1])
    xy_list.append(y)
    all_list.append(xy_list)
else:
  xy_list = []
  x = int(result.split(',')[0])
  xy_list.append(x)
  y = int(result.split(',')[1])
  xy_list.append(y)
  all_list.append(xy_list)
print(all_list)

最后嘛,我們得到了驗證碼的坐標,當然就去點擊啦。但是,這個坐標是相對于驗證碼的圖片的坐標,我們必須用ActionChains來移動一下動作鏈的位置。把他移動到驗證碼圖片的location。,然后點擊就ok了。此步驟的代碼如下:

# 循環遍歷點擊圖片
for i in all_list:
  x = i[0]
  y = i[1]
  action = ActionChains(driver).move_to_element_with_offset(code_img, x, y).click().perform()
  time.sleep(1)
driver.find_element_by_id('loginSub').click()

最后來看看全部代碼吧!!

這個代碼是超級鷹提供的接口。我封裝成一個類了。

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

下面是自己寫的,也就六七十行。

from selenium import webdriver
from chaojiying_Python.chaojiying import Chaojiying_Client
import time
from PIL import Image
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

# 實現無可視化界面的操作
# chrome_options = Options()
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome('D:\software\studySoftware\chromedriver_win32\chromedriver.exe')
driver.get('https://kyfw.12306.cn/otn/login/init')
# driver.maximize_window()
time.sleep(1)
driver.find_element_by_id('username').send_keys('用戶名')
time.sleep(0.5)
driver.find_element_by_id('password').send_keys('密碼')
# 將頁面進行截圖并保存
driver.save_screenshot('12306登錄頁面截圖.png')

# 確定驗證碼左上角和右下角的坐標
code_img = driver.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
location = code_img.location # 確定驗證碼圖片左上角的坐標
print('location:', location)
size = code_img.size # 確定驗證碼圖片的長和寬
print('size:', size)
rangle = (int(location['x']), int(location['y']), int(location['x']) + int(size['width']),
     int(location['y']) + int(size['height']))
print('rangle:', rangle)
i = Image.open('12306頁面截圖.png')
# 對指定區域裁剪
code_pic = i.crop(rangle)
file_name = 'code_pic.png'
code_pic.save(file_name)
time.sleep(2)
print('驗證碼圖片保存成功!!')
# 識別驗證坐標
chaojiying = Chaojiying_Client('用戶賬號', '密碼', '開發者賬號') # 用戶中心>>軟件ID 生成一個替換 96001
im = open('code_pic.png', 'rb').read() # 本地圖片文件路徑 來替換 a.jpg 有時WIN系統須要//
result = chaojiying.PostPic(im, 9004)['pic_str'] # 1902 驗證碼類型 官方網站>>價格體系 3.4+版 print 后要加()

all_list = [] # 存儲被點擊的坐標
if '|' in result:
  list1 = result.split('|')
  xy_list = []
  count1 = len(list1)
  for i in list1:
    x = int(list1[i].split(',')[0])
    xy_list.append(x)
    y = int(list1[i].split(',')[1])
    xy_list.append(y)
    all_list.append(xy_list)
else:
  xy_list = []
  x = int(result.split(',')[0])
  xy_list.append(x)
  y = int(result.split(',')[1])
  xy_list.append(y)
  all_list.append(xy_list)
print(all_list)
# 循環遍歷點擊圖片
for i in all_list:
  x = i[0]
  y = i[1]
  action = ActionChains(driver).move_to_element_with_offset(code_img, x, y).click().perform()
  time.sleep(1)
driver.find_element_by_id('loginSub').click()

到此這篇關于selenium+超級鷹實現模擬登錄12306的文章就介紹到這了,更多相關selenium 模擬登錄12306內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python+selenium實現12306模擬登錄的步驟
  • Selenium之模擬登錄鐵路12306的示例代碼
  • Python+Selenium+phantomjs實現網頁模擬登錄和截圖功能(windows環境)
  • selenium跳過webdriver檢測并模擬登錄淘寶
  • 使用selenium模擬登錄解決滑塊驗證問題的實現

標簽:周口 淮安 合肥 秦皇島 周口 綿陽 百色 綏化

巨人網絡通訊聲明:本文標題《selenium+超級鷹實現模擬登錄12306》,本文關鍵詞  selenium+,超級,鷹,實現,模擬,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《selenium+超級鷹實現模擬登錄12306》相關的同類信息!
  • 本頁收集關于selenium+超級鷹實現模擬登錄12306的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 浙江省| 凤山县| 岱山县| 建始县| 色达县| 雅江县| 玛纳斯县| 博白县| 墨竹工卡县| 阜城县| 宝清县| 浮山县| 新津县| 安塞县| 桦甸市| 玉山县| 凤庆县| 无锡市| 土默特右旗| 张北县| 元朗区| 饶平县| 西峡县| 陆丰市| 靖宇县| 遵化市| 临潭县| 高邑县| 镇平县| 特克斯县| 长乐市| 云林县| 庆安县| 周口市| 阳朔县| 昌宁县| 宁远县| 汨罗市| 邵武市| 和林格尔县| 沂南县|