利用python怎么绕过验证码爬取PTA的题目
创始人
2024-12-28 03:22:38
0次

思路开始:

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

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...