生活随笔
收集整理的這篇文章主要介紹了
使用selenium自动登陆滴滴打码网
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2020-7-2 by 微風
思路
1 使用webdriver調用谷歌瀏覽器,然后請求滴滴打碼網站。
2 通過selenium的xpath定位方法找到輸入賬號、密碼、驗證碼、登陸的位置,并且傳送具體的賬號、密碼給對應的位置。
3 對于傳送驗證碼的問題:首先需要重新單獨請求驗證碼,然后將驗證碼下載到本地,接著使用超級鷹來識別下載到本地的驗證碼,把識別后的驗證碼傳送到網頁中輸入驗證碼的位置。
4 點擊登陸的位置。
from selenium
import webdriver
import request#打開谷歌瀏覽器
driver
= webdriver
.Chrome(r
'D:\Python37\Lib\chromedriver.exe')
#請求滴滴打碼網站
driver
.get('http://www.ddocr.com/user/login.html')
#傳送賬號、密碼到相應的位置
driver
.find_element_by_xpath('//*[@id="account"]').send_keys('賬號')
driver
.find_element_by_xpath('//*[@id="password"]').send_keys('密碼')#重新單獨請求驗證碼
src
= driver
.find_element_by_xpath('//*[@id="verifyImg"]').get_attribute('src')
yanzhengma
= requests
.get(src
)
#將驗證碼下載到本地
with open('yanzhengma.jpg','wb') as file
:file
.write(yanzhengma
.content
)
#使用超級鷹識別下載到本地的驗證碼
#定義Chaojiying_Client類的代碼是超級鷹官網自帶的
from hashlib
import md5
class Chaojiying_Client(object
):def
__init__(self
, username
, password
, soft_id
):self
.username
= usernameself
.password
= md5(password
.encode('utf8')).hexdigest()self
.soft_id
= soft_idself
.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()
chaojiying
= Chaojiying_Client('賬號', '密碼', '軟件ID')
im
= open('yanzhengma.jpg', 'rb').read()#將超級鷹識別的驗證碼傳入網頁中輸入驗證碼的位置。
driver
.find_element_by_xpath('//*[@id="verity"]').send_keys(chaojiying
.PostPic(im
, 1004)['pic_str'])
#點擊登陸
driver
.find_element_by_xpath('//*[@id="userLogin"]/div[4]/button').click()
運行后的bug
運行后發現自動輸入驗證碼時與網頁上顯示的驗證碼并不一致。經過思考后,明白了問題出在思路的第三步。因為網頁上顯示的驗證碼是我們第一次請求滴滴打碼時的驗證碼,而自動輸入的驗證碼是我們第二次重新單獨請求的驗證碼。這兩次請求的驗證碼并不一致,所以就出現了這個bug。即思路的第三步并不可行。
解決的辦法
思路的第三步
1 使用selenium截取全屏,然后再截取全屏中特定區域(即驗證碼的區域)的圖片。
①使用selenium截取百度網頁全屏
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
browser.get('https://www.baidu.com')
time.sleep(3)# 用 get_screenshot_as_file('絕對路徑') 方法來截取全屏,并把截取的全屏保存到絕對路徑。
# 注:保存的圖片格式必須為png。
browser.get_screenshot_as_file('C:\Users\desktop\quanping.png')
②截取全屏中特定區域的圖片
from PIL import Image# 對驗證碼所在位置進行定位,注意定位時電腦的縮放布局必須要設置為100%,不然定位不準確。img = driver.find_element_by_xpath('code')time.sleep(2)# location屬性以字典的形式返回該圖片對象(既這張圖片)在瀏覽器中的坐標。假設圖片的坐標是(30,30) 即返回{‘x’:30,‘y’:30} 坐標軸是以屏幕左上角為原點,x軸向右遞增,y軸向下遞增。location = img.location# size屬性以字典的形式返回該圖片對象(即這張圖片)的寬度和高度。假設圖片的寬度和高度均為30,即返回{‘height’:30,‘width’:30}size = img.sizeleft = location['x']top = location['y']right = left + size['width']bottom = top + size['height']# pillow模塊使用crop((left, up, right, below))方法來裁剪圖片obj = Image.open('quanping.png')yanzhengma = obj.crop((left, top, right, bottom))# yanzhengma.show()# 把截取到的驗證碼圖片下載到本地。Image.save('yanzhengma.png')
2 使用超級鷹對下載到本地的驗證碼進行識別。
3 把超級鷹識別后的驗證碼傳送到網頁中輸入驗證碼的位置。
from selenium
import webdriver
import requests#打開谷歌瀏覽器
driver
= webdriver
.Chrome(r
'D:\Python37\Lib\chromedriver.exe')
#打開瀏覽器后發送
get請求
driver
.get('http://www.ddocr.com/user/login.html')
driver
.find_element_by_xpath('//*[@id="account"]').send_keys('賬號')
driver
.find_element_by_xpath('//*[@id="password"]').send_keys('密碼')from PIL import Image
driver
.get_screenshot_as_file('D:\PycharmProjects\爬蟲\quanping.png')
obj
= Image
.open('quanping.png')# 要注意截取驗證碼時電腦的縮放布局必須要設置為
100%,不然定位不準確。
location
= driver
.find_element_by_xpath('//*[@id="verifyImg"]').location
size
= driver
.find_element_by_xpath('//*[@id="verifyImg"]').sizeleft
= location
['x']
top
= location
['y']
right
= left
+ size
['width']
bottom
= top
+ size
['height']
yanzhengma
= obj
.crop((left
,top
,right
,bottom
))
yanzhengma
.save('yanzhengma.png')#使用超級鷹識別下載到本地的驗證碼
#定義Chaojiying_Client類的代碼是超級鷹官網自帶的
from hashlib
import md5
class Chaojiying_Client(object
):def
__init__(self
, username
, password
, soft_id
):self
.username
= usernameself
.password
= md5(password
.encode('utf8')).hexdigest()self
.soft_id
= soft_idself
.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()
chaojiying
= Chaojiying_Client('賬號', '密碼', '軟件ID')
im
= open('yanzhengma.png', 'rb').read()#將超級鷹識別的驗證碼傳入網頁中輸入驗證碼的位置。
driver
.find_element_by_xpath('//*[@id="verity"]').send_keys(chaojiying
.PostPic(im
, 1004)['pic_str'])
#點擊登陸
driver
.find_element_by_xpath('//*[@id="userLogin"]/div[4]/button').click()
總結
以上是生活随笔為你收集整理的使用selenium自动登陆滴滴打码网的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。