日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用selenium自动登陆滴滴打码网

發(fā)布時間:2023/12/20 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用selenium自动登陆滴滴打码网 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2020-7-2 by 微風(fēng)

思路

1 使用webdriver調(diào)用谷歌瀏覽器,然后請求滴滴打碼網(wǎng)站。
2 通過selenium的xpath定位方法找到輸入賬號、密碼、驗證碼、登陸的位置,并且傳送具體的賬號、密碼給對應(yīng)的位置。
3 對于傳送驗證碼的問題:首先需要重新單獨請求驗證碼,然后將驗證碼下載到本地,接著使用超級鷹來識別下載到本地的驗證碼,把識別后的驗證碼傳送到網(wǎng)頁中輸入驗證碼的位置。
4 點擊登陸的位置。

from selenium import webdriver import request#打開谷歌瀏覽器 driver = webdriver.Chrome(r'D:\Python37\Lib\chromedriver.exe') #請求滴滴打碼網(wǎng)站 driver.get('http://www.ddocr.com/user/login.html') #傳送賬號、密碼到相應(yīng)的位置 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類的代碼是超級鷹官網(wǎng)自帶的 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: 圖片字節(jié)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()#將超級鷹識別的驗證碼傳入網(wǎng)頁中輸入驗證碼的位置。 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

運行后發(fā)現(xiàn)自動輸入驗證碼時與網(wǎng)頁上顯示的驗證碼并不一致。經(jīng)過思考后,明白了問題出在思路的第三步。因為網(wǎng)頁上顯示的驗證碼是我們第一次請求滴滴打碼時的驗證碼,而自動輸入的驗證碼是我們第二次重新單獨請求的驗證碼。這兩次請求的驗證碼并不一致,所以就出現(xiàn)了這個bug。即思路的第三步并不可行。

解決的辦法

思路的第三步

1 使用selenium截取全屏,然后再截取全屏中特定區(qū)域(即驗證碼的區(qū)域)的圖片。
①使用selenium截取百度網(wǎng)頁全屏

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')

②截取全屏中特定區(qū)域的圖片

from PIL import Image# 對驗證碼所在位置進(jìn)行定位,注意定位時電腦的縮放布局必須要設(shè)置為100%,不然定位不準(zhǔn)確。img = driver.find_element_by_xpath('code')time.sleep(2)# location屬性以字典的形式返回該圖片對象(既這張圖片)在瀏覽器中的坐標(biāo)。假設(shè)圖片的坐標(biāo)是(30,30) 即返回{‘x’:30,‘y’:30} 坐標(biāo)軸是以屏幕左上角為原點,x軸向右遞增,y軸向下遞增。location = img.location# size屬性以字典的形式返回該圖片對象(即這張圖片)的寬度和高度。假設(shè)圖片的寬度和高度均為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 使用超級鷹對下載到本地的驗證碼進(jìn)行識別。
3 把超級鷹識別后的驗證碼傳送到網(wǎng)頁中輸入驗證碼的位置。

from selenium import webdriver import requests#打開谷歌瀏覽器 driver = webdriver.Chrome(r'D:\Python37\Lib\chromedriver.exe') #打開瀏覽器后發(fā)送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')# 要注意截取驗證碼時電腦的縮放布局必須要設(shè)置為100%,不然定位不準(zhǔn)確。 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類的代碼是超級鷹官網(wǎng)自帶的 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: 圖片字節(jié)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()#將超級鷹識別的驗證碼傳入網(wǎng)頁中輸入驗證碼的位置。 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()

總結(jié)

以上是生活随笔為你收集整理的使用selenium自动登陆滴滴打码网的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。