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

    技术2022-07-12  116

    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 = username self.password = md5(password.encode('utf8')).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() 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.size left = 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"]').size left = 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 = username self.password = md5(password.encode('utf8')).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() 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()
    Processed: 0.011, SQL: 9