python绕过图片滑动验证码实现爬取PTA所有题目功能 附源码


Posted in Python onJanuary 06, 2021

最近学了python爬虫,本着学以致用的态度去应用在生活中。突然发现算法的考试要来了,范围就是PTA刷过的题。让我一个个复制粘贴?不可能,必须爬它!

先开页面,人傻了,PTA的题目是异步加载的,爬了个寂寞(空数据)。AJAX我又不熟,突然想到了selenium。

selenium可以模拟人的操作让浏览器自动执行动作,具体的自己去了解,不多说了。干货来了:

登录界面有个图片的滑动验证码

python绕过图片滑动验证码实现爬取PTA所有题目功能 附源码

破解它的最好方式就是用opencv,opencv巨强,自己了解。
思路开始:
1.将背景图片和可滑动的图片下载
2.用opencv匹配这两张图片的最匹配位置,不用在意怎么实现的,算法极其BT,不是我这种数学不及格的人能想的。最终会得到一个匹配度最高的XY值
3.由于Y值不用考虑,拖动滑块是X值的事情,调用selenium里抓放的函数,把X值丢进去,让浏览器自动滑动即可。
注意:由于算法问题,可能不能一次成功,重启程序就行了,或者改动代码。
4.进去之后就用selenium各种操作爬就完事了
以下是源码:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import requests
import time
import numpy
import cv2
import os

#作者:许文鸿
#未经允许不可转载,转载时注明出处

#创建 WebDriver 对象,指明使用chrome浏览器驱动
web = webdriver.Chrome(r'd:\chromedriver.exe')
web.implicitly_wait(5)
#调用WebDriver 对象的get方法 可以让浏览器打开指定网址
web.get('https://pintia.cn/auth/login')
zh = web.find_element_by_xpath('/html/body/div[1]/div[3]/div/div[2]/form/div[1]/div[1]/div/div/div[1]/input')
mm = web.find_element_by_xpath('/html/body/div[1]/div[3]/div/div[2]/form/div[1]/div[2]/div/div/div[1]/input')

#在PTA的账号密码:
zh.send_keys('******@qq.com')
mm.send_keys('******')
#找到登录按钮并点击
web.find_element_by_xpath('/html/body/div[1]/div[3]/div/div[2]/form/div[2]/button/div/div').click()
#等待两秒,验证码加载完成
time.sleep(2)
#bg背景图片
bg_img_src = web.find_element_by_xpath(
 '/html/body/div[3]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/img[1]').get_attribute('src')
#front可拖动图片
front_img_src = web.find_element_by_xpath(
 '/html/body/div[3]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/img[2]').get_attribute('src')
#保存图片
with open("bg.jpg", mode="wb") as f:
 f.write(requests.get(bg_img_src).content)
with open("front.jpg", mode="wb") as f:
 f.write(requests.get(front_img_src).content)
#将图片加载至内存
bg = cv2.imread("bg.jpg")
front = cv2.imread("front.jpg")
js = 'alert("本人可能将此程序用于python课设,请靓仔靓女不要直接提交本人代码。即将报错,需要删除第42~44行代码即可正常运行");'
web.execute_script(js)
time.sleep(15)
#将背景图片转化为灰度图片,将三原色降维
bg = cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY)
#将可滑动图片转化为灰度图片,将三原色降维
front = cv2.cvtColor(front, cv2.COLOR_BGR2GRAY)
front = front[front.any(1)]
#用cv算法匹配精度最高的xy值
result = cv2.matchTemplate(bg, front, cv2.TM_CCOEFF_NORMED)
#numpy解析xy,注意xy与实际为相反,x=y,y=x
x, y = numpy.unravel_index(numpy.argmax(result), result.shape)
#找到可拖动区域
div = web.find_element_by_xpath('/html/body/div[3]/div[2]/div/div/div[2]/div/div[2]/div[2]')
#拖动滑块,以实际相反的y值代替x
ActionChains(web).drag_and_drop_by_offset(div, xoffset=y // 0.946, yoffset=0).perform()

#至此成功破解验证码,由于算法问题,准确率不能达到100%,可能需要多运行1~2次

for page in range(0, 1000):
 time.sleep(1)
 #此处的网址为PTA固定网页,仅需要更换page
 web.get('https://pintia.cn/problem-sets?tab=1&filter=all&page={page_}'.format(page_=page))
 #获取当前页面题目集网址,A_s为a标签的列表,urls用户存放网址
 A_s = web.find_elements_by_class_name('name_QIjv7')
 urls = []
 for a in A_s:
  urls.append(a.get_attribute('href'))
 #当页面不存在可爬取的网址,则退出程序
 if urls.__len__() == 0:
  print('爬取完成')
  os._exit()
 #对刚才获取的网址列表进行遍历
 for url in urls:
  web.get(url)
  #找到对应的题目对象
  tm = web.find_elements_by_css_selector("[class='problemStatusRect_3kpmC PROBLEM_ACCEPTED_1Dzzi']")
  tm_total = 0
  for i in range(0, 1000):
   # 遍历该页面的题型
   try:
    tm_type = web.find_element_by_xpath(
     '/html/body/div/div[3]/div[2]/div/div[2]/div[{i_}]/div/div[2]'.format(i_=i * 2 + 2)).text
    # 如果题型为编程/函数,记录对应的数量,方便后续爬取
    if tm_type == '编程题' or tm_type == '函数题':
     tm_total += int(web.find_element_by_xpath(
      '/html/body/div/div[3]/div[2]/div/div[2]/div[{i_}]/a/div/div'.format(i_=i * 2 + 2)).text[0])
   except:
    break
  # 根据函数/编程题数量取相应的题目对象,舍弃其他题目
  if tm_total != 0:
   tm = tm[-tm_total:]
  else:
   tm = []
  # 遍历剩余题目
  for tm_index in tm:
   try:
    tm_index.click()
    time.sleep(0.5)
    #获取题目中的代码
    tm_title = web.find_element_by_css_selector(
     "[class='text-center black-3 text-4 font-weight-bold my-3']").text
    mycode = web.find_element_by_css_selector('textarea').get_attribute('value')
    print('题目:' + tm_title)
    print(mycode)
    #接下来可以存入
   except:
    continue

到此这篇关于python绕过图片滑动验证码实现爬取PTA所有题目功能 附源码的文章就介绍到这了,更多相关python图片滑动验证码内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python实现多线程下载文件的代码实例
Jun 01 Python
Python中__init__和__new__的区别详解
Jul 09 Python
Python数据库的连接实现方法与注意事项
Feb 27 Python
快速入手Python字符编码
Aug 03 Python
Python日期的加减等操作的示例
Aug 15 Python
python生成n个元素的全组合方法
Nov 13 Python
python实现微信机器人: 登录微信、消息接收、自动回复功能
Apr 29 Python
详解python 内存优化
Aug 17 Python
Pandas替换及部分替换(replace)实现流程详解
Oct 12 Python
Python提取视频中图片的示例(按帧、按秒)
Oct 22 Python
python使用numpy中的size()函数实例用法详解
Jan 29 Python
pandas 操作 Excel操作总结
Mar 31 Python
python 获取谷歌浏览器保存的密码
Jan 06 #Python
python实现PolynomialFeatures多项式的方法
Jan 06 #Python
pytorch中index_select()的用法详解
Jan 06 #Python
Python之京东商品秒杀的实现示例
Jan 06 #Python
Python实现小黑屋游戏的完整实例
Jan 06 #Python
Jupyter Notebook 安装配置与使用详解
Jan 06 #Python
在Ubuntu中安装并配置Pycharm教程的实现方法
Jan 06 #Python
You might like
声音就能俘获人心,蕾姆,是哪个漂亮小姐姐配音呢?
2020/03/03 日漫
php 在线打包_支持子目录
2008/06/28 PHP
PHP实现取得HTTP请求的原文
2014/08/18 PHP
PHP链接MySQL的常用扩展函数
2014/10/23 PHP
PHP容器类的两种实现方式示例
2019/07/24 PHP
php查看一个变量的占用内存的实例代码
2020/03/29 PHP
轻量级的原生js日历插件calendar.js使用指南
2015/04/28 Javascript
JavaScript获取两个数组交集的方法
2015/06/09 Javascript
全面解析Angular中$Apply()及$Digest()的区别
2016/08/04 Javascript
自动化测试读写64位操作系统的注册表
2016/08/15 Javascript
JS扩展类,克隆对象与混合类实例分析
2016/11/26 Javascript
Angular 4 指令快速入门教程
2017/06/07 Javascript
vue-prop父组件向子组件进行传值的方法
2018/03/01 Javascript
手把手教你vue-cli单页到多页应用的方法
2018/05/31 Javascript
vue 自定义提示框(Toast)组件的实现代码
2018/08/17 Javascript
vue自定v-model实现表单数据双向绑定问题
2018/09/03 Javascript
js实现中文实时时钟
2020/01/15 Javascript
Vue开发环境跨域访问问题
2020/01/22 Javascript
对于Python中线程问题的简单讲解
2015/04/03 Python
python解决字符串倒序输出的问题
2018/06/25 Python
Python OOP类中的几种函数或方法总结
2019/02/22 Python
python通过paramiko复制远程文件及文件目录到本地
2019/04/30 Python
详解python运行三种方式
2019/05/13 Python
python基于paramiko将文件上传到服务器代码实现
2019/07/08 Python
草莓网美国官网:Strawberrynet USA
2016/12/11 全球购物
Speedo澳大利亚官网:全球领先游泳品牌
2018/02/04 全球购物
英国绿色商店:Natural Collection
2019/05/03 全球购物
ZWILLING双立人法国网上商店:德国刀具锅具厨具品牌
2019/08/28 全球购物
说出数据连接池的工作机制是什么?
2013/04/19 面试题
国外的一些J2EE面试题一
2012/10/13 面试题
中班开学寄语
2014/04/04 职场文书
地球一小时宣传标语
2014/06/24 职场文书
自我介绍演讲稿范文
2014/08/21 职场文书
政府会议通知范文
2015/04/15 职场文书
检讨书范文
2019/04/16 职场文书
Nginx的gzip相关介绍
2022/05/11 Servers