我正在编写一段代码来获取游戏包的标题和价格,将每个包的标题和价格分开,但它返回bundle_price为null.
import puppeteer from "puppeteer";
async function handleAgeRestriction(p) {
await p.evaluate(() => {
const select = document.querySelector("#ageYear");
const options = select.querySelectorAll("option");
const selectedOption = [...options].find(
(option) => option.text === "1900"
);
selectedOption.selected = true;
});
await p.click("#view_product_page_btn");
}
async function getDataFromGame() {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto("https://store.steampowered.com/app/271590/");
await handleAgeRestriction(page);
await page.waitForSelector(".apphub_AppName");
// await page.waitForSelector(".game_purchase_price");
await page.waitForSelector("div.game_area_purchase_game > h1");
await page.waitForSelector("div.discount_final_price");
const result = await page.evaluate(() => {
const data = document.querySelectorAll('.game_area_purchase_game');
const game = [...data].map((bundle) => {
const bundle_title = bundle.querySelector('div.game_area_purchase_game > h1').innerText;
const bundle_price = bundle.querySelector("div.discount_final_price").innerText;
return {
bundle_title,
bundle_price,
}
})
return game;
});
console.log(result);
await browser.close();
}
getDataFromGame();
我觉得这个错误很奇怪,因为如果我将"Bundle"替换为"Document",它将正确地检索价格,但它将始终是相同的价格
const result = await page.evaluate(() => {
const data = document.querySelectorAll('.game_area_purchase_game');
const game = [...data].map((bundle) => {
const bundle_title = bundle.querySelector('div.game_area_purchase_game > h1').innerText;
const bundle_price = document.querySelector("div.discount_final_price").innerText;
return {
bundle_title,
bundle_price,
}
})
return game;
});
console.log(result);
(这是一个例子,它不会给我一个错误,但价格将是相同的所有Bundle .