我使用nightmare (phantomjs wrapper) 来抓取网页,但出现错误:
幻影标准输出:TypeError:未定义不是构造函数(评估'$('a [href =“/dien-thoai/apple-iphone-6-plus-64gb-ban-quoc-te--pid975.html”]') .get(0).closest('section')')
var Nightmare = require('nightmare');
var bandcamp = new Nightmare()
.useragent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36")
.goto('http://viettelstore.vn/danh-muc/dien-thoai-010001.html#SpecOrder=')
.wait()
.click('a[href="/apple-mid1.html"]')
.wait()
.screenshot('viettelstore.png')
.evaluate(function () {
var x = $('a[href="/dien-thoai/apple-iphone-6-plus-64gb-ban-quoc-te--pid975.html"]').get(0).closest('section')
var y = $(x).find('.b-price.b-price_size3').find('.b-price__num').text()
return {name: y};
},function (value) {console.log(value.name);})
.run(function (err, nightmare) {
if (err) return console.log(err);
console.log('Done!');
});
单击后的页面屏幕短按预期正确。在浏览器的控制台中测试时,jquery 代码运行正常。所以我认为问题来自 phantomjs