有没有办法选择 <div>
按其在 Node Puppeteer 网络爬虫中的类?
有一个div
在如下所示的网页上:<div class="Body-body-qL80Q">
我希望我的抓取工具能够抓取 div
中的文本。目前,我只是尝试将其写入控制台以检查它是否获取了正确的文本。
我的querySelector
怎么了? ? (我之前让脚本导航到正确的页面并截取屏幕截图,它做得正确,所以我知道它的其余部分有效。)
const puppeteer = require('puppeteer');
const CREDS = require('./creds');
(async () => {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto('https://www.squarespace.com/login');
const USERNAME_SELECTOR = '.username.Input-hxTtdt.ipapEE';
const PASSWORD_SELECTOR = '.password.Input-hxTtdt.ipapEE';
const BUTTON_SELECTOR = '.Button-kDSBcD.fATVqu';
await page.click(USERNAME_SELECTOR);
await page.keyboard.type(CREDS.username);
await page.click(PASSWORD_SELECTOR);
await page.keyboard.type(CREDS.password);
await Promise.all([
page.waitForNavigation(),
page.click(BUTTON_SELECTOR),
]);
await page.goto('https://triangle-oarfish-hk88.squarespace.com/config/analytics#activity-log');
const textContent = await page.evaluate(() => document.querySelector('Body-body-qL80Q').className);
console.log(textContent);
await browser.close();
})();
这是错误:
(node:6116) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'className' of null
(node:6116) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6116) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
最佳答案
您忘记在 document.querySelector()
中的类选择器 Body-body-qL80Q
之前添加句点 .
里面的功能page.evaluate()
:
此外,您应该使用 textContent
属性而不是 className
属性。
您的常量textContent
应按以下方式初始化:
const textContent = await page.evaluate(() => document.querySelector('.Body-body-qL80Q').textContent);
关于javascript - 如何让 Puppeteer/Node 脚本按其类读取 <div> ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53199557/