0.1132021.02.26 12:17:42 字数 415 阅读 60
![python爬虫之滑动验证码[完整版] - 简书 - 图2](https://upload-images.jianshu.io/upload_images/5958956-20036059751abe2a.png#align=left&display=inline&height=1356&margin=%5Bobject%20Object%5D&originHeight=1356&originWidth=1920&status=done&style=none&width=1920)
爬虫的一大难点就是破解验证码。验证码大致上分为文字识别、滑动、文字点击、图像识别等,本文讲的是其中的滑动验证码。滑动验证码,需要我们将滑块移至图片缺口位置。
滑动验证码破解大致流程为:
1 - 启动浏览器,并进行截图,分割出验证码部分
2 - 将分割得到的验证码图片,发至超级鹰平台,获取返回的坐标
3 - 使用 selenium 模拟移动滑块
使用工具:python,selenium,chromedriver,chrome 浏览器
下面就是喜闻乐见的操作步骤讲解环节了(´◔౪◔)
代码部分:
from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport timefrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom PIL import Imageimport osimport syspath = os.path.dirname(os.path.dirname(__file__))sys.path.append(path)from chaojiying import Chaojiying_Clientimport requestsfrom hashlib import md5class main():def __init__(self):self.url = 'http://www.geetest.com/Register'self.file_path = './img/code_picture.png'self.file_path2 = './img/code_picture2.png'self.distance = 0self.key = 0def Launch_browser(self):self.driver = webdriver.Chrome()self.wait = WebDriverWait(self.driver, 10, 0.5)self.driver.get(self.url)Phone_Number = self.driver.find_element_by_xpath('/html/body/div[1]/div/div[8]/div/div[2]/div[1]/div[2]/div/div[2]/div[1]/input')Verification_Code = self.driver.find_element_by_xpath('/html/body/div[1]/div/div[8]/div/div[2]/div[1]/div[2]/div/div[2]/div[2]/input')Code_Button = self.driver.find_element_by_xpath('/html/body/div[1]/div/div[8]/div/div[2]/div[1]/div[2]/div/div[2]/div[2]/div[1]/div')Phone_Number.send_keys('12345678910')Code_Button.click()self.slider = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_slider_button')))def get_picture(self):self.driver.save_screenshot(self.file_path)def crop_picture(self):image = Image.open(self.file_path)weight, height = image.sizebox = (weight * 1/2 - 130, height * 1/2 - 130, weight * 1/2 + 130, height * 1/2 + 25)region = image.crop(box)region.save(self.file_path2)def cjy(self):self.chaojiying = Chaojiying_Client('xxx', 'xxx', 'xxx')im = open('./img/code_picture2.png', 'rb').read()re = self.chaojiying.PostPic(im, 9101)print(re)self.distance = int(re['pic_str'].split(',')[0]) - 25print(self.distance)self.im_id = re['pic_id']print(self.im_id)def get_track(self):self.track = []mid = self.distance * 4 / 5current = 0t = 0.2v = 0while current < self.distance:if current < mid:a = 8else:a = -12v0 = vv = v0 + a * tmove = v * t + 1 / 2 * a * t * tcurrent += moveself.track.append(round(move))print(self.track)def move(self):self.track = [self.distance]ActionChains(self.driver).click_and_hold(self.slider).perform()for x in self.track:ActionChains(self.driver).move_by_offset(xoffset=x, yoffset=0).perform()time.sleep(2)ActionChains(self.driver).release().perform()def check(self):if self.key ==1:passelse:re = self.chaojiying.ReportError(self.im_id)print(re)def quit(self):time.sleep(5)self.driver.quit()def main(self):self.Launch_browser()self.get_picture()self.crop_picture()self.cjy()self.move()self.check()self.quit()if __name__ == '__main__':ma = main()ma.main()
要点总结:
1 - 代码分为两个文件 main.py 和 chaojiying.py,chaojiying.py 为超级鹰平台的官方文件,下载后直接放在 main.py 的同一目录即可。
2 - 本篇为前三篇文章的总结,完整的解决了滑动验证码的问题。
3 - 获取坐标的部分交给了超级鹰平台处理。
那么本次的分享就到这里了,喜欢的话麻烦点赞关注一下;不喜欢的话可以去看下小编的其他文章,肯定有喜欢的;都不喜欢的话可以点个关注,万一以后有喜欢的呢 (๑•̀ㅂ•́)و✧
有疑问的小伙伴也可以在评论区留言哦,我会第一时间解答的。
![python爬虫之滑动验证码[完整版] - 简书 - 图3](/uploads/projects/forvs@python/a45382bd57ff19bf66659a1d778e686c.webp)
你点了吗◔ ‸◔?
更多精彩内容下载简书 APP
“对你有用,就是对我最大的支持”
还没有人赞赏,支持一下
总资产 6 (约 0.48 元) 共写了 1.2W 字获得 89 个赞共 25 个粉丝
被以下专题收入,发现更多相似内容
推荐阅读更多精彩内容
- 最近学了 python 爬虫,本着学以致用的态度去应用在生活中。突然发现算法的考试要来了,范围就是 PTA 刷过的题。让我…
![python爬虫之滑动验证码[完整版] - 简书 - 图5](/uploads/projects/forvs@python/bf39cc30af42627538c256a63d1d0971.webp)
- 工具地址:shellcodeloaderhttps://github.com/knownsec/shellcod...
[![python爬虫之滑动验证码[完整版] - 简书 - 图6](/uploads/projects/forvs@python/1839451e3ca469dcb2405c71263486f8.webp)
- 1.1 官网https://www.sudo.ws/download_mirrors.html[https://w..](https://www.sudo.ws/download_mirrors.html%5Bhttps://w..).
[ - 今天心血来潮,测试了下 Python 免杀 CS/MSF 中 Python 格式的 shellcode 主要是巧用 HEX 编码即可,…
![python爬虫之滑动验证码[完整版] - 简书 - 图8](/uploads/projects/forvs@python/d9b64f071a9da909b643925ec962e278.webp)
- 从效果图中可以看出,覆盖了 App 中常用的按钮效果,弧度、按压效果、标签选择、按钮渐变色等。如果用 shape.xml…
![python爬虫之滑动验证码[完整版] - 简书 - 图9](https://upload.jianshu.io/users/upload_avatars/22679253/27f36b40-ead3-422e-a457-1e893441e7be.jpg?imageMogr2/auto-orient/strip%7CimageView2/1/w/48/h/48/format/webp#align=left&display=inline&height=48&margin=%5Bobject%20Object%5D&originHeight=48&originWidth=48&status=done&style=none&width=48)
遇见编程阅读 1,723 评论 2 赞 32![python爬虫之滑动验证码[完整版] - 简书 - 图10](/uploads/projects/forvs@python/27381c1bd865ab5bd62cfa3c0978e29a.webp)
https://www.jianshu.com/p/a29e2e926f0f
![python爬虫之滑动验证码[完整版] - 简书 - 图1](/uploads/projects/forvs@python/644bb0028748c35926ee980cec296d32.webp)
![python爬虫之滑动验证码[完整版] - 简书 - 图4](/uploads/projects/forvs@python/f2b2e6bc4e5686c3c7075a3b65f8515c.webp)
