- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 puppeteer 抓取此页面:https://jcc.org/park-heights-indoor-pool-registration ,并将数据片段放入一个数组中(事件时间、标题、注册链接等)。
我将我正在抓取的页面的 html 复制到本地 html 文件中,一切正常(使用完全相同的代码!),但是对于 puppeteer,它返回一个空错误。最重要的是,当我选择单个元素时,收集所有数据时没有错误!
代码:
const puppeteer = require('puppeteer');
(async () => {
let jcc_url = 'https://jcc.org/park-heights-indoor-pool-registration';
let browser = await puppeteer.launch();
let page = await browser.newPage();
await page.goto(jcc_url, {waitUntil: 'networkidle0'});
let data = await page.evaluate(() => {
let slots_array = [];
$(".GXPEntry").each(function (index, element) {
slots_array[index] = {
index: index,
cancelled: undefined,
time: element.querySelector(".GXPTime").textContent,
title: element.querySelector('.GXPTitle').textContent,
link: element.querySelector('a.signUpGXP').getAttribute("href"),
availability: element.querySelector('div.GXPDescription span').textContent,
dayOfWeek: element.querySelector('a').getAttribute('data-date')
};
if (slots_array[index].title === "CANCELED: Lap Swimming - Men's Only"
||
slots_array[index].title === "CANCELED: Lap Swimming - Women's Only") {
slots_array[index].cancelled = true;
} else {
slots_array[index].cancelled = false;
}
});
return slots_array;
});
console.log(data);
await browser.close();
})();
这是我定位的页面的 HTML 布局:
<div class="GXPEntry">
<div class="GXPTime">8:15am-9:00am</div>
<div class="GXPTitle"><img src="https://groupexpro.com/schedule/logos/custom/logo_53760.jpg"
style="display: block; max-height: 30px; max-width: 120px; padding: 0px 5px 5px 0px;"
title="">Lap Swimming - Men's Only<span
style="position: relative; top: 2px; left: 4px;"><a class="signUpGXP removeIconGXP"
href="https://groupexpro.com/gxp/reservations/start/index/11814665/10/05/2020?e=1"
title="This class requires a reservation"><i
style="background-image: url('https://groupexpro.com/gxp/design/img/glyphicons-halflings.png'); background-position: -96px -72px; background-repeat: no-repeat; display: inline-block; height: 14px; vertical-align: text-top; width: 14px; position: relative; top: 0px; left: -4px; float: left; margin-right:6px; "></i></a></span>
</div>
<div class="GXPInstructor">Staff</div>
<div class="GXPStudio">Indoor Pool </div>
<div class="GXPCategory">Aquatics</div>
<div class="GXPLocation">Park Heights</div>
<div class="GXPDescription">
<a 11814665 alt="11814665" class="descGXP" data-date="10/05/2020" href="javascript://""="">Description</a>
|
<a alt="11814665" class="signUpGXP"
href="https://groupexpro.com/gxp/reservations/start/index/11814665/10/05/2020?e=1"
textmsg="3 SPOTS LEFT">
Sign Up</a>
<a alt="Add to Calendar" class="addToCalendar" href="#">
<img alt="Add to Calendar" border="0" height="14" src="https://groupexpro.com/schedule/embed/images/ics.gif">
</a>
<br><br><span>3 SPOTS LEFT</span>
</div>
我只是想从 .signUpGXP
类的链接中获取 href 数据,最后一个跨度标签“3 SPOTS LEFT”中的文本,div 中的标题文本.GXPTitle
,以及 div.GXPDescription
中第一个链接的 data-date
属性。
如果我将 HTML 复制到本地文件中,这对 jQuery 工作正常,但在 pupputeer 中它不起作用,并给我这个错误:
(node:22638) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'getAttribute' of null
at HTMLDivElement.<anonymous> (__puppeteer_evaluation_script__:12:59)
at Function.each (https://jcc.org/sites/default/files/js/js_POjCvph0DpQRBLbuAoUSghIegyfU_5lXHo4ESl4z0tw.js:2:2975)
at $.fn.init.each (https://jcc.org/sites/default/files/js/js_POjCvph0DpQRBLbuAoUSghIegyfU_5lXHo4ESl4z0tw.js:2:835)
at __puppeteer_evaluation_script__:5:24
at ExecutionContext._evaluateInternal (/Users/moshe/coding-workspace/jcc-ph-pool-register/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:217:19)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ExecutionContext.evaluate (/Users/moshe/coding-workspace/jcc-ph-pool-register/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:106:16)
at async /Users/moshe/coding-workspace/jcc-ph-pool-register/app.js:13:16
(node:22638) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:22638) [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.
不确定为什么找不到该属性。如果我这样做就完全没问题:
const puppeteer = require('puppeteer');
(async () => {
let jcc_url = 'https://jcc.org/park-heights-indoor-pool-registration';
let browser = await puppeteer.launch();
let page = await browser.newPage();
await page.goto(jcc_url, {waitUntil: 'networkidle2'});
let data = await page.evaluate(() => {
let time = document.querySelector('.GXPTime').innerText;
let title = document.querySelector('.GXPTitle').innerText;
let availability = document.querySelector('.GXPDescription span').innerText;
let link = document.querySelector('.signUpGXP').href;
let dayOfWeek = document.querySelector('.GXPDescription a').getAttribute('data-date');
return {
time,
title,
availability,
link,
dayOfWeek
}
});
console.log(data);
debugger;
await browser.close();
})();
我在这里获得了所有数据,但只有页面上的第一部分。
我将不胜感激。谢谢!
最佳答案
如果我在浏览器中运行评估函数,我会遇到同样的错误。看来问题是取消的事件没有注册链接。
关于javascript - querySelector 不适用于 puppeteer 中的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64204586/
这两种引用方法的主要区别是什么? 使用其中一种有什么好处?另外,它们各自最适合哪种用例? var selection = document.querySelector('.selector') !==
对于下面给出的代码,document.querySelector('createButton') 和 Polymer.dom(this.$).querySelector('createButton')
查看 mdn“querySelector”会在两个部分下弹出,但它们似乎都达到了相同的目的。哪一个是不同情况的理想选择? ...或者它们基本上在功能上是相同的? https://developer.m
这个问题已经有答案了: Is element.querySelector() different from document.querySelector()? (2 个回答) 已关闭 3 年前。 当我
我做了几次迭代测试来测试 Document.querySelector 的效率和 Element.querySelector . 标记: 脚本: 查询 Document.querySelec
如何使用 document.querySelectorAll() 从下面的代码中选择特定的文本输出? 我想在变量中捕获结果,并尝试使用以下代码来实现: let result = document.qu
HTML 2018-2019 如何使用 CSS 选择器定位元素 onclick 我试过下面的代码但不工作 ie.document.querySelector("[onclick='loadYearPa
通常在 javascript 中使用查询选择器时,我们会这样做, ABCD var className = 'abcd'; var x = document.querySelector('.'+cla
我目前正在学习 querySelector方法,惊讶地发现它被归类为过时了 这是 screenshot 的链接 querySelector 方法真的已经过时了吗?若有,原因为何?应该使用什么方法呢?
MDN 指定的 querySelectorALL() 不支持事件节点并且仅返回静态节点。 MDN querySelectorALL querySelector() 是否支持活节点 MDN 没有指定任何
这个问题在这里已经有了答案: What does this `…${…}…` code in the node docs mean? [duplicate] (1 个回答) 关闭 6 年前。 我是
根据下面的书面代码,当单击具有 keyCode 65 的“a”时,预期结果应该是在控制台中返回 DOM 元素“”。当我运行代码并检查它时,它在控制台中返回 null。 部分源码: wind
我正在使用 SVG.js select() 函数,它使用 querySelector() 函数。 目前,我使用的命令是:select("[id='1']")(1可以用其他数字代替) 我想做的是选择该元
我有这个循环应该设置 值为无并更新占位符。当我登录节点时它工作正常但是 value和 placeholder没有更新?怎么了? 数据是一个 JSON 对象。 var data = {"password
我正在尝试使用 QuerySelector 获取数据表单中每个评论的值。有一个“添加”按钮,允许显示另一个元素并将元素的名称增加 +1。例如,第一个评论的名称为“comment0”,第二个评论的名称为
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 7 年前。 Improv
我有一个具有以下格式类的输入字段: some_starting_constant_string somevariablestring some_ending_constant_string 所以我想要
我正在寻找一种方法来查找包含以给定字符串开头的属性的所有元素。例如: document.querySelectorAll('[ng-*]') 将返回带有 Angular 指令的所有元素(ng-clic
我的第一列应该是实际日期,但我的 js 脚本仅适用于我的 html 代码中的一个类。 问题:如何使我的js代码适用于html代码中的每个类,我使用querySelector(),但我应该使用类似$(t
有没有办法使用 CSS 选择器来选择出现在给定元素之后的元素。 假设我有一个像这样的 DOM A next span the target next span
我是一名优秀的程序员,十分优秀!