大佬教程收集整理的这篇文章主要介绍了使用 puppeteer 进行网页抓取找不到 CSS 标签,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我开始用 puppeteer 学习 JavaScript 中的网页抓取。我发现了一个我喜欢的视频,它展示了木偶操作者,我正在尝试抓取与视频 (link) 相同的信息。该页面与视频略有不同,因此我使用了我认为正确的标签。
当我尝试查找 "h3"
标签时出现问题。该标签存在于 DOM 中,但我的代码拒绝承认它的存在,但在查找 "h2"
标签时工作“正常”。
我想知道的是为什么我的代码没有检索到它。
网页:https://markeTingplatform.google.com/about/partners/find-a-partner?utm_source=markeTingplatform.google.com&utm_medium=et&utm_campaign=markeTingplatform.google.com%2Fabout%2Fpartners%2F
// normal things to launch it
const puppeteer = require("puppeteer");
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const url = "https://markeTingplatform.Google.com/about/partners/find-a-partner?utm_source=markeTingplatform.Google.com&utm_medium=et&utm_campaign=markeTingplatform.Google.com%2Fabout%2Fpartners%2F";
await page.goto(url);
// here comes the problem
// this doesn't work v
const h3 = await page.evaluate(() => document.querySELEctor("h3").textContent);
console.log(h3); //the error is because it trIEs to get the text content of null meaning it dIDn't found "h3"
// this DOES work v
const h2 = await page.evaluate(() => document.querySELEctor("h2").textContent);
console.log(h2);
//await browser.close();
})();
我知道 "h3"
存在。如果你能解释一下发生的事情,我将不胜感激,这样我就可以了解更多
谢谢。
页面上不存在h3标头,我们需要在waitForSELEctor
之前等待:
// normal things to launch it
const puppeteer = require("puppeteer");
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const url = "https://markeTingplatform.google.com/about/partners/find-a-partner?utm_source=markeTingplatform.google.com&utm_medium=et&utm_campaign=markeTingplatform.google.com%2Fabout%2Fpartners%2F";
await page.goto(url);
await page.waitForSELEctor('h3')
const h3 = await page.evaluate(() => document.querySELEctor("h3").textContent);
console.log(h3);
const h2 = await page.evaluate(() => document.querySELEctor("h2").textContent);
console.log(h2);
await browser.close(); // don't forget close it.
})();
输出为:
Viden
Find your perfect match.
以上是大佬教程为你收集整理的使用 puppeteer 进行网页抓取找不到 CSS 标签全部内容,希望文章能够帮你解决使用 puppeteer 进行网页抓取找不到 CSS 标签所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。