gpt4 book ai didi

nightmare - 未定义nightmarejs参数

转载 作者:行者123 更新时间:2023-12-03 13:24:10 30 4
gpt4 key购买 nike

我想抓取并通过页面中的每个herf获取html,然后输出到csv。现在我第一次使用 Nightmare 。所以我对未定义的段落有疑问。

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var fs = require('fs');
var result ;
nightmare
.goto('http://football-system.jp/fss/pub_kaijyolist.php?lid=h0xYuxqKQ+M=')
.wait(1000)
.evaluate(function () {
var divs = document.querySelectorAll('a[target="_blank"]'),i;
for (i = 0,result = ""; i < divs.length; ++i) {
result += divs[i].href.toString()+"\n";
}
return divs;
})
.end()
.then(function (divs) {
console.log(divs)
///////////////////////////////////////////////////
fs.writeFile('8.csv', result, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
////////////////////////////////////////////////////
})
.then(function() {
//since Nightmare has an internal `.then()`, return the instance returned by the final call in the chain
return nightmare
//click the next button to get the next page of search results
.goto(divs[0].href)
//get the first HREF from the second page of results
.evaluate(function() {
return document.querySelector('div[class="outputDate"]');
})
})
.catch(function (error) {
console.error('Search failed:', error);
});

第二个goto()有一个错误:未定义divs。非常感谢。

最佳答案

错误消息说明了一切:divs未定义。这意味着在第二个divs回调的范围内没有then变量。

您可以将其中的两个then回调“合并”为一个,并具有如下所示的内容:

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var fs = require('fs');
var result ;
nightmare
.goto('http://football-system.jp/fss/pub_kaijyolist.php?lid=h0xYuxqKQ+M=')
.wait(1000)
.evaluate(function () {
var divs = document.querySelectorAll('a[target="_blank"]'),i;
for (i = 0,result = ""; i < divs.length; ++i) {
result += divs[i].href.toString()+"\n";
}
return divs;
})
.then(function (divs) {
console.log(divs)
///////////////////////////////////////////////////
fs.writeFile('8.csv', result, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
////////////////////////////////////////////////////
nightmare
//click the next button to get the next page of search results
console.log('this is the link I want to navigate to', divs[0].href)
.goto(divs[0].href)
//get the first HREF from the second page of results
.evaluate(function() {
return document.querySelector('div[class="outputDate"]');
})
.then(function(div) {
console.log('I got the "div", now Ill do something with it', div)
})
})
.catch(function (error) {
console.error('Search failed:', error);
});

nightmare.end()

但是此代码也将失败,因为您的 divs列表中充满了空对象。我更改了一些代码以使其对您有用一半:
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var fs = require('fs');
var result ;
nightmare
.goto('http://football-system.jp/fss/pub_kaijyolist.php?lid=h0xYuxqKQ+M=')
.wait(1000)
.evaluate(function () {
var divs = document.querySelectorAll('a[target="_blank"]'),i;
var links = []
for (i = 0,result = ""; i < divs.length; ++i) {
links.push(divs[i].href)
result += divs[i].href.toString()+"\n";
}
return links;
})
.then(function (links) {
console.log(links)
///////////////////////////////////////////////////
fs.writeFile('8.csv', links.join('\n'), function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
////////////////////////////////////////////////////
console.log('this is the link I want to navigate to', links[0])
nightmare
//click the next button to get the next page of search results
.goto(links[0])
.wait(5000)
//get the first HREF from the second page of results
.evaluate(function() {
return document.querySelector('div[class="outputDate"]');
})
.end()
.then(function(div) {
console.log('I got the "div", now Ill do something with it', div)
})
})
.catch(function (error) {
console.error('Search failed:', error);
});

现在,您将不得不遍历链接列表并导航到每个链接。这与Nightmare有点棘手,请查看 this资源和 this答案。我相信您会发现它们很有帮助。

关于nightmare - 未定义nightmarejs参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40279104/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com