You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中实现玩家信息连续查询的循环执行问题

实现Node.js玩家信息连续查询功能

原代码仅支持单次查询,添加while循环时因未正确处理异步逻辑导致重复询问异常。以下是修复后的完整实现,同时优化资源使用并增加退出机制:

console.clear();

const puppeteer = require('puppeteer');
const prompt = require("prompt-sync")({ sigint: true });

console.log(`
_______  _______  _______  ___            _______  __   __  _______ 
|       ||       ||   _   ||   |          |       ||  |_|  ||       |
|       ||   _   ||  |_|  ||   |          |    ___||       ||    ___|
|       ||  | |  ||       ||   |          |   |___ |       ||   |___ 
|      _||  |_|  ||       ||   |___  ___  |    ___| |     | |    ___|
|     |_ |       ||   _   ||       ||   | |   |___ |   _   ||   |___ 
|_______||_______||__| |__||_______||___| |_______||__| |__||_______|
                                     
`);

// 主函数:处理连续查询
async function runContinuousQueries() {
    // 只启动一次浏览器,复用实例减少资源消耗
    const browser = await puppeteer.launch();

    while (true) {
        const name = prompt("Player (输入'exit'退出): ");
        
        // 退出条件
        if (name.toLowerCase() === 'exit') {
            console.log("退出查询程序");
            await browser.close();
            break;
        }

        if (!name.trim()) {
            console.log("请输入有效的玩家名称");
            continue;
        }

        try {
            await scrapeProduct(browser, `https://bwstats.shivam.pro/user/${name}`);
        } catch (error) {
            console.log(`查询失败:${error.message}`);
            console.log("---------------------------------------------");
        }
    }
}

async function scrapeProduct(browser, url) {
    const page = await browser.newPage();
    await page.goto(url);

    // 获取等级
    const [el2] = await page.$x('/html/body/div/main/div[1]/div[2]/p[1]');
    const txt2 = await el2.getProperty('textContent');
    const lvl = await txt2.jsonValue();

    // 获取FKDR
    const [el] = await page.$x('/html/body/div/main/div[1]/div[2]/p[3]');
    const txt = await el.getProperty('textContent');
    const fkdr = await txt.jsonValue();

    // 获取BBLR
    const [el1] = await page.$x('/html/body/div/main/div[1]/div[2]/p[4]');
    const txt1 = await el1.getProperty('textContent');
    const BBLR = await txt1.jsonValue();

    // 获取WLR
    const [el3] = await page.$x('/html/body/div/main/div[1]/div[2]/div/table/tbody/tr[4]/td[2]');
    const txt3 = await el3.getProperty('textContent');
    const wlr = await txt3.jsonValue();

    console.log(lvl);
    console.log(fkdr);
    console.log(BBLR);
    console.log("Win/Loss Ratio (WLR): " + wlr);
    console.log("---------------------------------------------");

    // 关闭当前页面,释放资源
    await page.close();
}

// 启动程序
runContinuousQueries();

关键修改说明

  • 异步循环处理:使用async/await配合while(true)循环,确保每次查询完成后再提示输入下一个玩家,避免异步操作导致的重复询问。
  • 浏览器复用:仅启动一次浏览器实例,每次查询创建新页面并在查询完成后关闭,大幅减少资源占用。
  • 退出机制:支持输入'exit'退出程序,提升用户体验。
  • 异常捕获:添加try/catch处理查询过程中的错误(如玩家不存在、页面元素找不到等),避免程序崩溃。
  • 输入校验:判断空输入,提示用户输入有效名称。

内容的提问来源于stack exchange,提问作者user20393634

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 00:10:44