当前位置:   article > 正文

Python爬虫--实现图片验证码全自动输入_自动填充图片验证码的代码

自动填充图片验证码的代码

爬虫--实现图片验证码全自动输入

爬取网站:豆瓣(https://accounts.douban.com/login

爬虫思路:1. 使用selenium, 对图片验证码进行截图操作,

                  2. 接入打码平台--云打码,传输图片,返回验证码

 

一. 爬虫代码如下:

  1. import time
  2. from selenium import webdriver
  3. from PIL import Image
  4. from io import BytesIO # 以IO的形式转换为二进制
  5. import YDM_test
  6. class DouBan(object):
  7. def __init__(self):
  8. self.url = 'https://accounts.douban.com/login'
  9. self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
  10. def run(self):
  11. # 创建chrome对象, 发送get请求
  12. chrome = webdriver.Chrome('/home/python/Desktop/chromedriver')
  13. chrome.get(self.url)
  14. # 获取输入框元素, 输入账号密码
  15. time.sleep(1)
  16. chrome.find_element_by_id('email').send_keys('your_count@163.com')
  17. chrome.find_element_by_id('password').send_keys('your_pwd')
  18. # 截图打码 --> 重点
  19. screen_shot = chrome.get_screenshot_as_png() # 获取全屏截图的对象
  20. screen_image = Image.open(BytesIO(screen_shot)) # 以IO的形式转换为二进制
  21. screen_image.save('00_screen_image.png')
  22. # 对验证码图片区域进行截图,保存
  23. code_image_el = chrome.find_element_by_id('captcha_image') # 获取验证码图片对象
  24. height = code_image_el.size['height']
  25. width = code_image_el.size['width']
  26. left = code_image_el.location['x']
  27. top = code_image_el.location['y']
  28. right = left + width
  29. bottom = top + height
  30. # 验证码图片--区域位置信息
  31. cut_info = (left, top, right, bottom)
  32. print(cut_info)
  33. # 截图操作,保存截图
  34. cut_image = screen_image.crop(cut_info)
  35. cut_image.save('00_cut_image.png')
  36. # 全自动打码--云打码平台
  37. code = YDM_test.image_decode('00_cut_image.png')
  38. chrome.find_element_by_id('captcha_field').send_keys(code)
  39. # 点击登陆
  40. chrome.find_element_by_name('login').click()
  41. # 退出浏览器
  42. time.sleep(3)
  43. chrome.quit()
  44. if __name__ == '__main__':
  45. db = DouBan()
  46. db.run()

 

二. 云打码SDK接口文件 --> YDM_test.py

  1. import http.client, mimetypes, urllib, json, time, requests
  2. ######################################################################
  3. class YDMHttp:
  4. apiurl = 'http://api.yundama.com/api.php'
  5. username = ''
  6. password = ''
  7. appid = ''
  8. appkey = ''
  9. def __init__(self, username, password, appid, appkey):
  10. self.username = username
  11. self.password = password
  12. self.appid = str(appid)
  13. self.appkey = appkey
  14. def request(self, fields, files=[]):
  15. response = self.post_url(self.apiurl, fields, files)
  16. response = json.loads(response)
  17. return response
  18. def balance(self):
  19. data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
  20. response = self.request(data)
  21. if (response):
  22. if (response['ret'] and response['ret'] < 0):
  23. return response['ret']
  24. else:
  25. return response['balance']
  26. else:
  27. return -9001
  28. def login(self):
  29. data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
  30. response = self.request(data)
  31. if (response):
  32. if (response['ret'] and response['ret'] < 0):
  33. return response['ret']
  34. else:
  35. return response['uid']
  36. else:
  37. return -9001
  38. def upload(self, filename, codetype, timeout):
  39. data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
  40. file = {'file': filename}
  41. response = self.request(data, file)
  42. if (response):
  43. if (response['ret'] and response['ret'] < 0):
  44. return response['ret']
  45. else:
  46. return response['cid']
  47. else:
  48. return -9001
  49. def result(self, cid):
  50. data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid)}
  51. response = self.request(data)
  52. return response and response['text'] or ''
  53. def decode(self, filename, codetype, timeout):
  54. cid = self.upload(filename, codetype, timeout)
  55. if (cid > 0):
  56. for i in range(0, timeout):
  57. result = self.result(cid)
  58. if (result != ''):
  59. return cid, result
  60. else:
  61. time.sleep(1)
  62. return -3003, ''
  63. else:
  64. return cid, ''
  65. def report(self, cid):
  66. data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
  67. response = self.request(data)
  68. if (response):
  69. return response['ret']
  70. else:
  71. return -9001
  72. def post_url(self, url, fields, files=[]):
  73. for key in files:
  74. files[key] = open(files[key], 'rb')
  75. res = requests.post(url, files=files, data=fields)
  76. return res.text
  77. ######################################################################
  78. # 用户名
  79. username = '注册的用户账号'
  80. # 密码
  81. password = '账号密码'
  82. # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
  83. appid = 4999 # 对应的在平台中创建的软件id
  84. # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
  85. appkey = '需要在云打码平台中创建获取'
  86. # 超时时间,秒
  87. timeout = 30
  88. # 封装调用接口--image_decode()
  89. def image_decode(filename, codetype=3000):
  90. # 检查
  91. if (username == 'username'):
  92. print('请设置好相关参数再测试')
  93. else:
  94. # 初始化
  95. yundama = YDMHttp(username, password, appid, appkey)
  96. # 登陆云打码
  97. uid = yundama.login()
  98. print('uid: %s' % uid)
  99. # 查询余额
  100. balance = yundama.balance()
  101. print('balance: %s' % balance)
  102. # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
  103. cid, result = yundama.decode(filename, codetype, timeout)
  104. print('cid: %s, result: %s' % (cid, result))
  105. return result
  106. ######################################################################
  107. if __name__ == '__main__':
  108. image_decode('00_cut_image.png')

 

 ------------------END-------------------------

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/287342
推荐阅读
相关标签
  

闽ICP备14008679号