基于2025年平台反爬升级的解决方案(时效性适配)
技术实现核心思路
目标:通过模拟人类操作(随机间隔、行为轨迹、自然停顿)降低被反爬识别的概率。
分步实现代码(含详细注释)
pythonCopy Code from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver import ActionChains from webdriver_manager.chrome import ChromeDriverManager import time import random import numpy as np # 配置浏览器参数(规避无头模式特征) options = webdriver.ChromeOptions() options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36...") # 自定义UA options.add_experimental_option("excludeSwitches", ["enable-automation"]) # 隐藏自动化标识 driver = webdriver.Chrome(ChromeDriverManager().install(), options=options) # 人类行为模拟函数 def human_like_delay(min=2, max=8): """生成接近人类操作的随机延迟(高斯分布更真实)""" delay = np.random.normal(loc=(min+max)/2, scale=1.5) delay = max(min, min(max, delay)) # 限制范围 time.sleep(delay) def human_like_mouse(element): """模拟人类鼠标移动轨迹(非线性)""" action = ActionChains(driver) action.move_to_element(element).perform() time.sleep(random.uniform(0.5, 1.2)) # 悬停观察 # 登录流程 driver.get("https://慕课平台登录页") human_like_delay(3, 5) # 等待页面加载 # 输入账号密码(随机分段输入) username = driver.find_element(By.ID, "username") for char in "your_username": username.send_keys(char) time.sleep(random.uniform(0.1, 0.3)) # 模拟打字间隔 human_like_delay(1, 2) password = driver.find_element(By.ID, "password") password.send_keys("your_password") # 密码快速输入(符合人类习惯) # 点击登录(带随机偏移点击位置) login_btn = driver.find_element(By.CLASS_NAME, "login-btn") action = ActionChains(driver) action.move_to_element_with_offset(login_btn, random.randint(-5,5), random.randint(-5,5)).click().perform() # 进入课程页(等待网络请求完成) human_like_delay(8, 15) # 模拟页面切换思考时间 driver.get("课程页面URL") # 视频播放自动化 video = driver.find_element(By.CSS_SELECTOR, ".video-container") human_like_mouse(video) # 模拟鼠标悬停 video.click() # 随机执行副操作(提升真实性) if random.random() > 0.7: # 30%概率触发 driver.execute_script("window.scrollBy(0, 200)") # 滚动页面 human_like_delay(2, 4) driver.find_element(By.CLASS_NAME, "fake-comment-btn").click() # 随机点赞/评论 # 心跳维持(模拟观看行为) while True: time_out = random.randint(180, 300) # 间隔3-5分钟 time.sleep(time_out) # 随机触发互动(避免完全静止) if random.random() > 0.8: driver.find_element(By.CLASS_NAME, "video-player").click() # 暂停/播放 human_like_delay(1, 3) # 检查课程是否完成(根据页面元素判断) if "已完成" in driver.page_source: break driver.quit()
关键优化点
-
反反爬策略
- 浏览器指纹隐藏:通过
excludeSwitches
禁用自动化标识,自定义UA规避检测。 - 非匀速操作:使用高斯分布生成延迟时间,比均匀分布更接近真人。
- 浏览器指纹隐藏:通过
-
行为模拟增强
- 分段输入:账号逐个字符输入,密码快速输入(符合用户习惯差异)。
- 鼠标轨迹:通过
ActionChains
模拟非线性移动,避免直线定位。 - 随机偏移点击:点击按钮时加入位置抖动。
-
副操作干扰
- 随机滚动页面、点赞、评论等非核心操作,干扰行为分析模型。
风险规避建议
-
频率控制
原文链接:http://www.36sw.com/rd/40429.html,转载和复制请保留此链接。
以上就是关于Python time random 模拟人类行为 自动刷课全部的内容,关注我们,带您了解更多相关内容。
以上就是关于Python time random 模拟人类行为 自动刷课全部的内容,关注我们,带您了解更多相关内容。