1. 程式人生 > >Python 實現全自動登入(真正的全自動,自動識別驗證碼)

Python 實現全自動登入(真正的全自動,自動識別驗證碼)

你沒有看錯,全自動驗證~~~

黑科技?還是黑程式碼?
我感覺這個看在你用啥,對不對?反正我用來(* * * * ) 你懂得

好了,先說一下用到的東西

  • selenium (本意是用來全自動測試)
  • Phantomjs (一種沒有介面的瀏覽器)
  • ** 驗證碼識別器(一塊錢可用100次的這種)

關門放程式碼

from selenium import webdriver
from PIL import Image
if __name__ == '__main__':
wbe = webdriver.PhantomJS()
wbe.get("https://www.某個網站的登入頁面.com/login/index.html"
)//你可以拿知乎,百度,等等測試
element = wbe.find_element_by_xpath('//*[@id="entry_name"]/p[3]/img')//驗證碼所在的xpath路徑
left = element.location['x']
top = element.location['y']
right = element.location['x'] + element.size['width']
bottom = element.location['y'] + element.size['height']
im = Image.open(r'登入頁.png'
)//全頁面截圖
im = im.crop((left, top, right, bottom))
im.save('驗證碼.png')
#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
class RClient(object):
def __init__(self, username, password, soft_id, soft_key):
self.username = username
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.soft_key = soft_key
self.base_params = {
'username'
: self.username,
'password': self.password,
'softid': self.soft_id,
'softkey': self.soft_key,
}
self.headers = {
'Connection': 'Keep-Alive',
'Expect': '100-continue',
'User-Agent': 'ben',
}
def rk_create(self, im, im_type, timeout=60):
"""
im: 圖片位元組
im_type: 題目型別
"""

params = {
'typeid': im_type,
'timeout': timeout,
}
params.update(self.base_params)
files = {'image': ('a.png', im)}
r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
return r.json()
def rk_report_error(self, im_id):
"""
im_id:報錯題目的ID
"""

params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://api.ruokuai.com/reporterror.json', data=params, headers=self.headers)
return r.json()
def get_code():
rc = RClient('使用者名稱', '密碼', '94522', '62c235939b7240879453f31603733fd6')//想拿下測試的留言我,教你拿到測試賬號
im = open('a.png', 'rb').read()
print rc.rk_create(im, 3040)
  • 完整程式碼
#!/usr/bin/env python
# coding:utf-8
from selenium import webdriver
from PIL import Image
import requests
from hashlib import md5
import time
class RClient(object):
def __init__(self, username, password, soft_id, soft_key):
self.username = username
self.password = md5(password.encode("utf-8")).hexdigest()
self.soft_id = soft_id
self.soft_key = soft_key
self.base_params = {
'username': self.username,
'password': self.password,
'softid': self.soft_id,
'softkey': self.soft_key,
}
self.headers = {
'Connection': 'Keep-Alive',
'Expect': '100-continue',
'User-Agent': 'ben',
}
def rk_create(self, im, im_type, timeout=60):
"""
im: 圖片位元組
im_type: 題目型別
"""

params = {
'typeid': im_type,
'timeout': timeout,
}
params.update(self.base_params)
files = {'image': ('a.png', im)}
r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
return r.json()
def rk_report_error(self, im_id):
"""
im_id:報錯題目的ID
"""

params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://api.ruokuai.com/reporterror.json', data=params, headers=self.headers)
return r.json()
def get_code(im_file):
rc = RClient('賬號', '密碼', '94522', '62c235939b7240879453f31603733fd6')
im_source = open(im_file, "rb").read()
print(rc.rk_create(im_source, 3040))
if __name__ == '__main__':
wbe = webdriver.PhantomJS()
wbe.get("https://www.dajiang365.com/login/index.html")
time.sleep(2)
wbe.save_screenshot("das.png")
element = wbe.find_element_by_xpath('//*[@id="entry_name"]/p[3]/img')
left = element.location['x']
top = element.location['y']
right = element.location['x'] + element.size['width']
bottom = element.location['y'] + element.size['height']
im = Image.open(r'das.png')
im = im.crop((left, top, right, bottom))
im.save('a.png')
time.sleep(2)
get_code("a.png")