「编程类软件工具合集」
链接:https://pan.quark.cn/s/0b6102d9a66a
一、为什么需要网页自动化?
每天手动重复填写表单、点击按钮、下载文件?这些机械操作不仅浪费时间,还容易出错。网页自动化就像给浏览器装上"数字助手",能自动完成点击、输入、抓取数据等任务。典型应用场景包括:
- 电商价格监控:自动抓取竞品价格并生成报表
- 社交媒体管理:定时发布内容并统计互动数据
- 测试用例执行:自动完成Web应用的回归测试
- 数据采集:从网页提取结构化信息用于分析
二、核心工具对比与选择
1. Selenium:全能选手
- 适用场景:需要模拟真实用户操作的复杂页面
- 优势:支持所有主流浏览器,能处理JavaScript渲染的动态内容
- 示例代码:
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://www.example.com") search_box = driver.find_element(By.NAME, "q") search_box.send_keys("Python自动化") search_box.submit()
2. Requests+BeautifulSoup:轻量级组合
- 适用场景:静态页面数据抓取
- 优势:速度快,资源消耗小
- 示例代码:
import requests from bs4 import BeautifulSoup response = requests.get("https://books.toscrape.com/") soup = BeautifulSoup(response.text, 'html.parser') books = soup.select(".product_pod h3 a") for book in books: print(book["title"])
3. Playwright:新兴黑马
- 适用场景:现代Web应用测试
- 优势:自动等待元素加载,支持多语言
- 示例代码:
from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch() page = browser.new_page() page.goto("https://twitter.com/") page.fill('input[name="session[username_or_email]"]', "your_username") page.press('input[name="session[username_or_email]"]', 'Enter')
三、浏览器自动化实战技巧
1. 元素定位策略
- ID定位:最稳定的方式,如
driver.find_element(By.ID, "username") - CSS选择器:适合复杂结构,如
div.content > p.highlight - XPath:当其他方式失效时使用,如
//button[contains(text(),'提交')] - 相对定位:Playwright特有,基于可见文本定位
2. 等待机制处理
- 显式等待:推荐方式,设置条件和时间限制
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "dynamic-content")) ) - 隐式等待:全局设置,不推荐单独使用
- 智能等待:Playwright默认自动等待元素可交互
3. 交互操作进阶
- 文件上传:
upload = driver.find_element(By.XPATH, "//input[@type='file']") upload.send_keys("/path/to/file.jpg") - 鼠标悬停:
from selenium.webdriver.common.action_chains import ActionChains menu = driver.find_element(By.ID, "dropdown-menu") ActionChains(driver).move_to_element(menu).perform() - 键盘操作:
from selenium.webdriver.common.keys import Keys search = driver.find_element(By.NAME, "q") search.send_keys("Python" + Keys.ENTER)
4. 多窗口/标签页处理
# 打开新窗口 driver.execute_script("window.open('https://www.google.com');") # 切换窗口 windows = driver.window_handles driver.switch_to.window(windows[1])四、数据采集与处理
1. 动态内容加载
- 分析网络请求:通过Chrome开发者工具的Network面板,找到数据接口直接请求
import requests url = "https://api.example.com/data" headers = {"Authorization": "Bearer xxx"} response = requests.get(url, headers=headers).json() - 无头浏览器渲染:对SPA应用使用无头模式获取完整DOM
from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("--headless") driver = webdriver.Chrome(options=options)
2. 数据清洗与存储
- 结构化提取:
products = [] for item in soup.select(".product-item"): products.append({ "name": item.select_one(".title").text.strip(), "price": item.select_one(".price").text, "rating": item["data-rating"] }) - 存储方案选择:
- 小数据量:CSV文件
- 中等数据:SQLite数据库
- 大数据:MongoDB或MySQL
3. 反爬策略应对
- 请求头伪装:
headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", "Referer": "https://www.example.com/", "Accept-Language": "zh-CN,zh;q=0.9" } - IP代理池:(可使用站大爷IP池)
import random proxies = [ "http://123.123.123.123:8080", "http://124.124.124.124:8080" ] proxy = random.choice(proxies) response = requests.get(url, headers=headers, proxies={"http": proxy}) - 行为模拟:
- 随机延迟:
time.sleep(random.uniform(1, 3)) - 鼠标轨迹:记录真实用户操作轨迹并重放
- 随机延迟:
五、自动化测试实战案例
1. 测试用例设计
以登录功能为例:
import pytest @pytest.mark.parametrize("username,password,expected", [ ("valid_user", "correct_pwd", True), ("invalid_user", "wrong_pwd", False), ("", "", False) ]) def test_login(username, password, expected): driver.get("/login") driver.find_element(By.ID, "username").send_keys(username) driver.find_element(By.ID, "password").send_keys(password) driver.find_element(By.ID, "submit").click() if expected: assert "Welcome" in driver.page_source else: assert "Error" in driver.page_source2. 测试报告生成
使用pytest-html插件:
pytest test_login.py --html=report.html3. CI/CD集成
在GitHub Actions中配置自动化测试:
name: Web Test on: [push] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 - name: Install dependencies run: pip install selenium pytest - name: Run tests run: pytest test_login.py -v六、高级应用场景
1. 自动化报表生成
结合Pandas和Matplotlib:
import pandas as pd import matplotlib.pyplot as plt data = pd.DataFrame(products) price_stats = data.groupby("category")["price"].agg(["mean", "count"]) plt.figure(figsize=(10, 6)) price_stats["mean"].plot(kind="bar") plt.title("Average Price by Category") plt.savefig("price_report.png")2. 定时任务调度
使用APScheduler:
from apscheduler.schedulers.blocking import BlockingScheduler def job(): print("Running daily data collection...") # 自动化脚本代码 scheduler = BlockingScheduler() scheduler.add_job(job, 'cron', hour=8, minute=30) scheduler.start()3. 跨平台兼容处理
检测操作系统并适配路径:
import os import platform def get_download_path(): if platform.system() == "Windows": return os.path.join(os.environ["USERPROFILE"], "Downloads") else: return os.path.join(os.path.expanduser("~"), "Downloads")常见问题Q&A
Q1:Selenium报错"ElementNotInteractableException"怎么办?
A:通常有三种解决方案:
- 添加显式等待确保元素可交互
- 使用JavaScript直接操作元素:
driver.execute_script("arguments[0].click();", element) - 检查元素是否在iframe中,需要先切换:
driver.switch_to.frame("iframe_name")
Q2:如何处理登录验证码?
A:根据验证码类型选择不同方案:
- 简单图形验证码:使用Tesseract OCR识别
import pytesseract from PIL import Image element = driver.find_element(By.ID, "captcha") element.screenshot("captcha.png") code = pytesseract.image_to_string(Image.open("captcha.png")) - 复杂验证码:接入第三方打码平台
- 短信验证码:使用模拟器或接收转发服务
Q3:自动化脚本运行不稳定如何解决?
A:从以下方面排查:
- 增加重试机制:
from tenacity import retry, stop_after_attempt, wait_fixed @retry(stop=stop_after_attempt(3), wait=wait_fixed(2)) def click_element(driver, locator): driver.find_element(*locator).click() - 使用更稳定的定位方式(优先ID/CSS选择器)
- 捕获并处理所有可能的异常
Q4:如何同时操作多个浏览器窗口?
A:使用多线程或异步方案:
from concurrent.futures import ThreadPoolExecutor def run_browser(url): driver = webdriver.Chrome() driver.get(url) # 操作代码 with ThreadPoolExecutor(max_workers=3) as executor: executor.submit(run_browser, "https://example.com") executor.submit(run_browser, "https://google.com")Q5:自动化脚本如何避免被网站检测?
A:综合使用以下技术:
- 浏览器指纹伪装:修改canvas、WebGL等硬件特征
- 请求参数随机化:时间戳、排序等
- 行为模式模拟:随机鼠标移动、滚动等
- 使用无头浏览器时添加用户配置:
options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"])
这个自动化工具箱涵盖了从基础操作到高级应用的完整知识体系。实际项目中,建议从简单场景入手,逐步增加复杂度。以电商价格监控为例,完整实现流程可能是:定时启动脚本→打开商品页面→等待价格加载→提取价格数据→存储到数据库→生成价格趋势图→发送通知邮件。通过不断迭代优化,每个环节都可以实现高度自动化。