怎么用脚本实现自动翻页

wen 实用脚本 2

本文目录导读:

怎么用脚本实现自动翻页

  1. 浏览器控制台脚本(最简单)
  2. 油猴脚本(Tampermonkey)
  3. Python 自动化脚本
  4. 使用 Playwright
  5. 智能自适应脚本
  6. 使用建议

我来介绍几种实现自动翻页的脚本方法,从简单到复杂:

浏览器控制台脚本(最简单)

使用 setTimeout 定时翻页

// 自动翻页脚本 - 控制台直接粘贴运行
let page = 1;
const maxPages = 10; // 最大翻页数
const delay = 2000;  // 每次间隔毫秒
function autoPagination() {
    if (page >= maxPages) {
        console.log('翻页完成!');
        return;
    }
    // 方式1:点击下一页按钮
    const nextBtn = document.querySelector('.next') || 
                   document.querySelector('[class*="next"]') ||
                   document.querySelector('a[rel="next"]');
    if (nextBtn) {
        nextBtn.click();
        page++;
        console.log(`正在翻到第 ${page} 页...`);
        setTimeout(autoPagination, delay);
    } else {
        console.log('找不到下一页按钮');
    }
}
// 方式2:通过滚动到底部自动加载
function autoScrollPage() {
    window.scrollTo(0, document.body.scrollHeight);
    setTimeout(() => {
        if (document.body.scrollHeight > window.scrollY + window.innerHeight) {
            autoScrollPage();
        } else if (page < maxPages) {
            page++;
            autoScrollPage();
        }
    }, 1500);
}
// 方式3:根据特定选择器判断
function smartPagination() {
    if (page >= maxPages) {
        console.log('完成');
        return;
    }
    // 查找常见的翻页按钮
    const selectors = [
        '.pagination .next',
        '.page-next',
        '.btn-page-next',
        'button:contains("下一页")',
        '[aria-label="下一页"]'
    ];
    let btnFound = false;
    for (const selector of selectors) {
        const btn = document.querySelector(selector);
        if (btn) {
            btn.click();
            btnFound = true;
            break;
        }
    }
    if (btnFound) {
        page++;
        setTimeout(smartPagination, delay);
    }
}
// 执行
autoPagination();

油猴脚本(Tampermonkey)

// ==UserScript==
// @name         自动翻页助手
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  自动翻页工具
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==
(function() {
    'use strict';
    let settings = {
        enabled: false,
        interval: 2000,  // 翻页间隔
        maxPages: 50,    // 最大翻页数
        currentPage: 1
    };
    // 创建控制面板
    function createControlPanel() {
        const panel = document.createElement('div');
        panel.style.cssText = `
            position: fixed;
            top: 10px;
            right: 10px;
            background: #fff;
            border: 1px solid #ccc;
            padding: 10px;
            border-radius: 5px;
            z-index: 9999;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        `;
        panel.innerHTML = `
            <h4 style="margin: 0 0 10px">自动翻页控制</h4>
            <button id="startBtn">开始翻页</button>
            <button id="stopBtn">停止翻页</button>
            <br><br>
            <label>
                间隔(秒):
                <input type="number" id="interval" value="2" min="0.5" step="0.5">
            </label>
            <br><br>
            <label>
                最大页数:
                <input type="number" id="maxPages" value="50" min="1">
            </label>
        `;
        document.body.appendChild(panel);
        bindEvents();
    }
    function bindEvents() {
        document.getElementById('startBtn').addEventListener('click', start);
        document.getElementById('stopBtn').addEventListener('click', stop);
    }
    function start() {
        settings.enabled = true;
        settings.interval = document.getElementById('interval').value * 1000;
        settings.maxPages = parseInt(document.getElementById('maxPages').value);
        console.log('开始自动翻页...');
        paginate();
    }
    function stop() {
        settings.enabled = false;
        console.log('停止自动翻页');
    }
    function paginate() {
        if (!settings.enabled || settings.currentPage >= settings.maxPages) {
            console.log('翻页结束');
            return;
        }
        const nextBtn = findNextButton();
        if (nextBtn) {
            nextBtn.click();
            settings.currentPage++;
            setTimeout(paginate, settings.interval);
        } else {
            // 尝试滚动加载
            window.scrollTo(0, document.body.scrollHeight);
            setTimeout(paginate, settings.interval + 500);
        }
    }
    function findNextButton() {
        const selectors = [
            '.next', '.btn-next',
            '.pagination .next',
            '[rel="next"]',
            'a:contains("下一页")',
            '.page-next'
        ];
        for (const selector of selectors) {
            const element = document.querySelector(selector);
            if (element) return element;
        }
        return null;
    }
    // 初始化
    createControlPanel();
})();

Python 自动化脚本

使用 Selenium

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
def auto_pagination(url, max_pages=10, delay=2):
    """
    使用Selenium实现自动翻页
    """
    driver = webdriver.Chrome()  # 或 Firefox()
    driver.get(url)
    try:
        for page in range(1, max_pages + 1):
            print(f"正在处理第 {page} 页...")
            # 获取页面内容
            content = driver.find_elements(By.XPATH, "//div[@class='content']")
            # 处理你的数据...
            # 查找下一页按钮
            next_btn = None
            selectors = [
                ".//a[contains(text(), '下一页')]",
                ".//a[@rel='next']",
                ".//button[contains(text(), '下一页')]"
            ]
            for selector in selectors:
                try:
                    next_btn = driver.find_element(By.XPATH, selector)
                    if next_btn:
                        break
                except:
                    continue
            if next_btn:
                next_btn.click()
                time.sleep(delay)  # 等待页面加载
            else:
                print("未找到下一页按钮")
                break
    finally:
        driver.quit()
# 使用示例
# auto_pagination('https://example.com', max_pages=5)

使用 Playwright

from playwright.sync_api import sync_playwright
import time
def auto_pagination_playwright(url, max_pages=10):
    """
    使用Playwright实现自动翻页
    """
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)  # headless=True 为无头模式
        page = browser.new_page()
        page.goto(url)
        for i in range(1, max_pages + 1):
            print(f"处理第 {i} 页")
            # 等待内容加载
            page.wait_for_load_state("networkidle")
            # 查找下一页按钮
            try:
                next_button = page.locator("text=下一页").first
                if not next_button.is_visible():
                    # 尝试其他选择器
                    next_button = page.locator("[rel='next']").first
                next_button.click()
                time.sleep(2)  # 等待加载
            except:
                print("没有找到下一页按钮")
                break
        browser.close()
# 使用
# auto_pagination_playwright('https://example.com')

智能自适应脚本

// 智能检测翻页方式
class AutoPagination {
    constructor(maxPages = 20, interval = 2000) {
        this.maxPages = maxPages;
        this.interval = interval;
        this.currentPage = 1;
        this.running = false;
        this.method = null;
    }
    start() {
        this.running = true;
        this.detectMethod();
        this.paginate();
    }
    detectMethod() {
        // 检测翻页方式
        if (document.querySelector('.pagination')) {
            this.method = 'pagination';
        } else if (this.isScrollLoad()) {
            this.method = 'scroll';
        } else {
            this.method = 'click';
        }
    }
    isScrollLoad() {
        // 检测是否支持滚动加载
        return document.querySelector('.infinite-scroll') || 
               document.querySelector('#load-more');
    }
    async paginate() {
        if (!this.running || this.currentPage >= this.maxPages) {
            this.stop();
            return;
        }
        console.log(`自动翻页中: 第 ${this.currentPage} 页`);
        switch(this.method) {
            case 'pagination':
                this.clickPagination();
                break;
            case 'scroll':
                this.scrollLoad();
                break;
            case 'click':
                this.loadMore();
                break;
        }
        this.currentPage++;
        setTimeout(() => this.paginate(), this.interval);
    }
    clickPagination() {
        const nextBtn = document.querySelector('.pagination .next, .pagination a[rel="next"]');
        if (nextBtn) nextBtn.click();
    }
    scrollLoad() {
        window.scrollTo(0, document.body.scrollHeight);
    }
    loadMore() {
        const loadMoreBtn = document.querySelector('#load-more, .load-more');
        if (loadMoreBtn) loadMoreBtn.click();
    }
    stop() {
        this.running = false;
        console.log('自动翻页完成');
    }
}
// 使用
const autoPaginator = new AutoPagination(10, 2000);
autoPaginator.start();

使用建议

  1. 性能优化:添加随机延迟避免请求过快
  2. 异常处理:检测页面加载失败时重试
  3. 数据保存:及时保存获取到的数据
  4. 遵守规则:不要超出robots.txt允许的范围

这些脚本可以根据具体的网站结构调整,选择最适合的方式使用。

抱歉,评论功能暂时关闭!