网页自动化脚本selenium防检测

引言

在当今数字化时代,Web自动化测试已成为提升开发效

理解优化目标

明确需要优化的对象(如网站、算法、业务流程等),确定核心指标(如加载速度、转化率、成本等)。分析当前性能瓶颈或可改进点,通过数据收集(如日志、用户反馈)定位问题根源。

技术优化策略

代码层面:减少冗余计算,使用高效算法(如将冒泡排序改为快速排序)。缓存频繁访问的数据,避免重复请求。示例代码:

python 复制代码
# 优化前:双重循环导致O(n²)复杂度
for i in range(len(data)):
    for j in range(len(data)):
        process(data[i], data[j])

# 优化后:预处理降低复杂度
processed = [precompute(x) for x in data]
for item in processed:
    execute(item)

资源管理 :压缩静态资源(如图片、CSS/JS文件),启用CDN加速。数据库优化包括索引添加、查询重构,如将SELECT *改为指定字段。

用户体验优化

缩短关键路径(如注册流程从5步减至3步),采用A/B测试验证设计改动。确保移动端适配,减少页面加载时间(目标2秒内)。

监控与迭代

部署实时监控工具(如Google Analytics、Prometheus),跟踪优化后指标变化。建立定期复盘机制,根据数据持续调整策略。

工具推荐

  • 性能分析:Lighthouse、WebPageTest
  • 代码优化:Py-Spy(Python性能分析)、Chrome DevTools
  • 自动化:GitHub Actions(CI/CD流程优化)

通过多维度协同改进,平衡短期收益与长期可维护性。

理解优化目标

明确需要优化的对象(如网站、算法、业务流程等),确定核心指标(如加载速度、转化率、成本等)。分析当前性能瓶颈或可改进点,通过数据收集(如日志、用户反馈)定位问题根源。

技术优化策略

代码层面:减少冗余计算,使用高效算法(如将冒泡排序改为快速排序)。缓存频繁访问的数据,避免重复请求。示例代码:

python 复制代码
# 优化前:双重循环导致O(n²)复杂度
for i in range(len(data)):
    for j in range(len(data)):
        process(data[i], data[j])

# 优化后:预处理降低复杂度
processed = [precompute(x) for x in data]
for item in processed:
    execute(item)

资源管理 :压缩静态资源(如图片、CSS/JS文件),启用CDN加速。数据库优化包括索引添加、查询重构,如将SELECT *改为指定字段。

用户体验优化

缩短关键路径(如注册流程从5步减至3步),采用A/B测试验证设计改动。确保移动端适配,减少页面加载时间(目标2秒内)。

监控与迭代

部署实时监控工具(如Google Analytics、Prometheus),跟踪优化后指标变化。建立定期复盘机制,根据数据持续调整策略。

工具推荐

  • 性能分析:Lighthouse、WebPageTest
  • 代码优化:Py-Spy(Python性能分析)、Chrome DevTools
  • 自动化:GitHub Actions(CI/CD流程优化)

通过多维度协同改进,平衡短期收益与长期可维护性。

率和数据获取能力的重要手段。Selenium作为最流行的Web自动化测试框架之一,能够帮助开发者模拟用户操作、抓取网页数据,甚至实现自动化测试流程。本文将以起点中文网(qidian.com)的分类数据抓取为例,详细介绍如何使用Selenium进行Web自动化操作。

环境准备

1. 安装必要库

复制代码

bash

|------------------------|
| pip install selenium |

2. 下载浏览器驱动

代码实现详解

1. 浏览器初始化

复制代码

python

|------------------------------------------------------------------------------------------------------------|
| from selenium import webdriver |
| from selenium.webdriver.chrome.service import Service as ChromeService |
| from selenium.webdriver.edge.service import Service as EdgeService |
| |
| def start_chrome(): |
| chrome = ChromeService(executable_path="C:/Program Files/Google/Chrome/Application/chrome.exe") |
| options = webdriver.ChromeOptions() |
| options.add_experimental_option("excludeSwitches", ["enable-automation"]) |
| options.add_argument("--disable-blink-features=AutomationControlled") |
| driver = webdriver.Chrome(service=chrome, options=options) |
| return driver |
| |
| def start_edge(): |
| edge = EdgeService(executable_path='C:/Program Files (x86)/Microsoft/Edge/Application/msedgedriver.exe') |
| options = webdriver.EdgeOptions() |
| options.add_experimental_option("excludeSwitches", ["enable-automation"]) |
| options.add_argument("--disable-blink-features=AutomationControlled") |
| driver = webdriver.Edge(service=edge, options=options) |
| return driver |

关键点

  • 使用Service类指定浏览器驱动路径
  • 通过Options配置隐藏自动化特征,防止被网站检测
  • 支持Chrome和Edge两种浏览器选择

2. 主流程实现

复制代码

python

|--------------------------------------------------------------------|
| from time import sleep |
| from selenium.webdriver.common.by import By |
| |
| def run_main(): |
| # 1. 打开浏览器 |
| driver = start_edge() # 或使用start_chrome() |
| |
| try: |
| # 2. 访问目标网站 |
| driver.get("https://www.qidian.com/") |
| sleep(2) # 等待页面加载 |
| |
| # 3. 作品分类处理 |
| classify_element = driver.find_element(By.ID, 'classify-list') |
| dl_element = classify_element.find_element(By.CLASS_NAME, 'cf') |
| dd_elements = dl_element.find_elements(By.TAG_NAME, 'dd') |
| |
| if dd_elements: |
| for dd in dd_elements: |
| # 三种元素定位方式(任选其一) |
| # 方法1:直接使用标签名(最简单) |
| i_text = dd.find_element(By.TAG_NAME, 'i').text |
| b_text = dd.find_element(By.TAG_NAME, 'b').text |
| a_href = dd.find_element(By.TAG_NAME, 'a').get_attribute('href') |
| |
| print(f"分类图标: {i_text}, 分类名称: {b_text}, 链接: {a_href}") |
| |
| finally: |
| # 4. 关闭浏览器 |
| driver.quit() |

3. 元素定位方式对比

代码中展示了三种元素定位方式,实际应用中可根据需求选择:

方式 示例 特点
直接标签 By.TAG_NAME 最简单直接,但可能不够精确
CSS选择器 By.CSS_SELECTOR 灵活强大,推荐使用
XPath By.XPATH 最精确但性能稍差

完整代码

复制代码

python

|------------------------------------------------------------------------------------------------------------|
| #!/usr/bin/env python |
| # -*- coding: utf-8 -*- |
| from time import sleep |
| from selenium import webdriver |
| from selenium.webdriver.chrome.service import Service as ChromeService |
| from selenium.webdriver.common.by import By |
| from selenium.webdriver.edge.service import Service as EdgeService |
| |
| def start_chrome(): |
| chrome = ChromeService(executable_path="C:/Program Files/Google/Chrome/Application/chrome.exe") |
| options = webdriver.ChromeOptions() |
| options.add_experimental_option("excludeSwitches", ["enable-automation"]) |
| options.add_argument("--disable-blink-features=AutomationControlled") |
| driver = webdriver.Chrome(service=chrome, options=options) |
| return driver |
| |
| def start_edge(): |
| edge = EdgeService(executable_path='C:/Program Files (x86)/Microsoft/Edge/Application/msedgedriver.exe') |
| options = webdriver.EdgeOptions() |
| options.add_experimental_option("excludeSwitches", ["enable-automation"]) |
| options.add_argument("--disable-blink-features=AutomationControlled") |
| driver = webdriver.Edge(service=edge, options=options) |
| return driver |
| |
| def run_main(): |
| driver = start_edge() |
| |
| try: |
| driver.get("https://www.qidian.com/") |
| sleep(2) |
| |
| classify_element = driver.find_element(By.ID, 'classify-list') |
| dl_element = classify_element.find_element(By.CLASS_NAME, 'cf') |
| dd_elements = dl_element.find_elements(By.TAG_NAME, 'dd') |
| |
| if dd_elements: |
| for dd in dd_elements: |
| try: |
| i_text = dd.find_element(By.TAG_NAME, 'i').text |
| b_text = dd.find_element(By.TAG_NAME, 'b').text |
| a_href = dd.find_element(By.TAG_NAME, 'a').get_attribute('href') |
| print(f"分类图标: {i_text}, 分类名称: {b_text}, 链接: {a_href}") |
| except Exception as e: |
| print(f"处理分类时出错: {e}") |
| |
| except Exception as e: |
| print(f"程序运行出错: {e}") |
| finally: |
| driver.quit() |
| |
| if __name__ == '__main__': |
| run_main() |

最佳实践建议

  1. 异常处理:添加try-except块处理可能的异常
  2. 显式等待 :使用WebDriverWait替代sleep()提高效率
  3. 日志记录:添加日志记录功能便于调试
  4. 配置分离:将路径等配置信息提取到配置文件
  5. 无头模式 :生产环境可使用无头模式(headless)

扩展功能

  1. 数据存储:将抓取的数据保存到CSV或数据库
  2. 定时任务 :结合schedule库实现定时抓取
  3. 多线程:使用多线程提高抓取效率
  4. 反爬策略:添加随机延迟、User-Agent轮换等

总结

本文通过起点中文网分类数据抓取的实例,展示了Selenium的基本用法和最佳实践。Selenium不仅可以用于测试,还能广泛应用于数据抓取、自动化操作等场景。掌握Selenium的核心概念和常用方法后,你可以轻松实现各种Web自动化需求。

进阶学习建议

  • 学习Selenium Grid实现分布式测试
  • 探索Appium进行移动端自动化
  • 结合Python其他库(如pandas)进行数据处理

希望本文能为你的Web自动化之旅提供有价值的参考!

相关推荐
zyl837211 小时前
Docker 使用手册
运维·docker·容器
古月方枘Fry1 小时前
MGRE实验
运维·服务器
stolentime2 小时前
FreeDomain 本地开发环境快速搭建指南
运维·服务器·网络
bush43 小时前
嵌入式linux学习记录四
linux·运维·学习
lihao lihao4 小时前
软硬链接
linux·运维·服务器
TOWE technology4 小时前
智能安防监控系统如何做好防雷?——视频信号SPD综合应用方案解析
运维·服务器·防雷产品·信号保护·信号防雷·spd
楼田莉子4 小时前
Docker学习:Docker介绍及其架构介绍
运维·后端·学习·docker·容器·架构
大明者省5 小时前
IIS 端口绑定正常访问的原理说明与常见误区澄清
运维·服务器·笔记
晚风吹红霞5 小时前
Linux软件包管理器详解 —— yum与apt的使用及软件生态
linux·运维·服务器
曦夜日长5 小时前
Linux系统篇,进程概念(一):计算机体系、操作系统的认识、程序的加载过程
linux·运维·网络