【发布时间】:2021-09-08 18:01:43
【问题描述】:
我正在使用 nightmareJS 并尝试使用 document.getElementsByClassName 进行网络抓取,这是代码:
import Nightmare from 'nightmare'
const nightmare = Nightmare({ show: true })
var name = name
nightmare
.goto('https://www.amazon.com/')
.insert("input[aria-label='Search']", 'impressora')
.click("input[value='Go']")
.wait(2000)
.evaluate(function(){
let text = document.getElementsByClassName('a-offscreen')[0];
name = text;
return name;
}).then(function (name) {
console.log('Price:', name)
});
输出是这样的:
Price: [object HTMLSpanElement]
我想得到的是“a-offscreen”类中的价格。任何人都可以帮忙吗?谢谢。
【问题讨论】:
-
return document.querySelector('.a-offscreen span').innerText;(您正在尝试打印元素本身,而不是其中的文本)
标签: javascript web-scraping nightmare