如何用Puppeteer通过类名获取Valorant追踪页面的最近比赛文本?
提取Valorant Tracker页面的最近比赛文本
我想要从Valorant Tracker的指定用户页面中,通过类名提取“最近比赛”(last match)的文本内容,以下是我编写的Puppeteer实现代码:
const puppeteer = require('puppeteer'); const url = 'https://tracker.gg/valorant/profile/riot/SYNCO%23PYNCO/overview'; async function run() { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto(url); const weapon = await page.evaluate(() => { const weapon = document.querySelector('.weapon .weapon__main-stat').innerText; return weapon; }); console.log(weapon); // 等待页面加载完成 await page.waitForSelector('iframe'); // 获取iframe元素 const iframe = await page.$('iframe'); // 切换到iframe的上下文 const frame = await iframe.contentFrame(); // 等待iframe内的元素加载完成 await frame.waitForSelector('.timeline-match'); // 获取第一个timeline-match元素的文本 const elementText = await frame.$eval('.timeline-match', element => element.innerText); console.log(elementText); await browser.close(); } run();
内容的提问来源于stack exchange,提问作者Ahmad Aljoudi
相关产品推荐
相关产品推荐

