python爬虫之滑动验证码[完整版] - 简书 - 图1

0.1132021.02.26 12:17:42 字数 415 阅读 60

python爬虫之滑动验证码[完整版] - 简书 - 图2

爬虫的一大难点就是破解验证码。验证码大致上分为文字识别、滑动、文字点击、图像识别等,本文讲的是其中的滑动验证码。滑动验证码,需要我们将滑块移至图片缺口位置。

滑动验证码破解大致流程为:

1 - 启动浏览器,并进行截图,分割出验证码部分

2 - 将分割得到的验证码图片,发至超级鹰平台,获取返回的坐标

3 - 使用 selenium 模拟移动滑块

使用工具:python,selenium,chromedriver,chrome 浏览器

下面就是喜闻乐见的操作步骤讲解环节了(´◔౪◔)

代码部分:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.action_chains import ActionChains
  3. import time
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.support import expected_conditions as EC
  7. from PIL import Image
  8. import os
  9. import sys
  10. path = os.path.dirname(os.path.dirname(__file__))
  11. sys.path.append(path)
  12. from chaojiying import Chaojiying_Client
  13. import requests
  14. from hashlib import md5
  15. class main():
  16. def __init__(self):
  17. self.url = 'http://www.geetest.com/Register'
  18. self.file_path = './img/code_picture.png'
  19. self.file_path2 = './img/code_picture2.png'
  20. self.distance = 0
  21. self.key = 0
  22. def Launch_browser(self):
  23. self.driver = webdriver.Chrome()
  24. self.wait = WebDriverWait(self.driver, 10, 0.5)
  25. self.driver.get(self.url)
  26. Phone_Number = self.driver.find_element_by_xpath(
  27. '/html/body/div[1]/div/div[8]/div/div[2]/div[1]/div[2]/div/div[2]/div[1]/input')
  28. Verification_Code = self.driver.find_element_by_xpath(
  29. '/html/body/div[1]/div/div[8]/div/div[2]/div[1]/div[2]/div/div[2]/div[2]/input')
  30. Code_Button = self.driver.find_element_by_xpath(
  31. '/html/body/div[1]/div/div[8]/div/div[2]/div[1]/div[2]/div/div[2]/div[2]/div[1]/div')
  32. Phone_Number.send_keys('12345678910')
  33. Code_Button.click()
  34. self.slider = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_slider_button')))
  35. def get_picture(self):
  36. self.driver.save_screenshot(self.file_path)
  37. def crop_picture(self):
  38. image = Image.open(self.file_path)
  39. weight, height = image.size
  40. box = (weight * 1/2 - 130, height * 1/2 - 130, weight * 1/2 + 130, height * 1/2 + 25)
  41. region = image.crop(box)
  42. region.save(self.file_path2)
  43. def cjy(self):
  44. self.chaojiying = Chaojiying_Client('xxx', 'xxx', 'xxx')
  45. im = open('./img/code_picture2.png', 'rb').read()
  46. re = self.chaojiying.PostPic(im, 9101)
  47. print(re)
  48. self.distance = int(re['pic_str'].split(',')[0]) - 25
  49. print(self.distance)
  50. self.im_id = re['pic_id']
  51. print(self.im_id)
  52. def get_track(self):
  53. self.track = []
  54. mid = self.distance * 4 / 5
  55. current = 0
  56. t = 0.2
  57. v = 0
  58. while current < self.distance:
  59. if current < mid:
  60. a = 8
  61. else:
  62. a = -12
  63. v0 = v
  64. v = v0 + a * t
  65. move = v * t + 1 / 2 * a * t * t
  66. current += move
  67. self.track.append(round(move))
  68. print(self.track)
  69. def move(self):
  70. self.track = [self.distance]
  71. ActionChains(self.driver).click_and_hold(self.slider).perform()
  72. for x in self.track:
  73. ActionChains(self.driver).move_by_offset(xoffset=x, yoffset=0).perform()
  74. time.sleep(2)
  75. ActionChains(self.driver).release().perform()
  76. def check(self):
  77. if self.key ==1:
  78. pass
  79. else:
  80. re = self.chaojiying.ReportError(self.im_id)
  81. print(re)
  82. def quit(self):
  83. time.sleep(5)
  84. self.driver.quit()
  85. def main(self):
  86. self.Launch_browser()
  87. self.get_picture()
  88. self.crop_picture()
  89. self.cjy()
  90. self.move()
  91. self.check()
  92. self.quit()
  93. if __name__ == '__main__':
  94. ma = main()
  95. ma.main()

要点总结:
1 - 代码分为两个文件 main.py 和 chaojiying.py,chaojiying.py 为超级鹰平台的官方文件,下载后直接放在 main.py 的同一目录即可。
2 - 本篇为前三篇文章的总结,完整的解决了滑动验证码的问题。
3 - 获取坐标的部分交给了超级鹰平台处理。

那么本次的分享就到这里了,喜欢的话麻烦点赞关注一下;不喜欢的话可以去看下小编的其他文章,肯定有喜欢的;都不喜欢的话可以点个关注,万一以后有喜欢的呢 (๑•̀ㅂ•́)و✧

有疑问的小伙伴也可以在评论区留言哦,我会第一时间解答的。

python爬虫之滑动验证码[完整版] - 简书 - 图3

你点了吗◔ ‸◔?

更多精彩内容下载简书 APP

“对你有用,就是对我最大的支持”

还没有人赞赏,支持一下

python爬虫之滑动验证码[完整版] - 简书 - 图4

总资产 6 (约 0.48 元) 共写了 1.2W 字获得 89 个赞共 25 个粉丝

被以下专题收入,发现更多相似内容

推荐阅读更多精彩内容