我正在寫在Node.js的Twitter的殭屍,但我有一個使用名爲「scrapejs」的NPM庫中的函數它抓住從雅虎財經數據和工作得很好,問題是,雖然我以後的功能加載來自botton的代碼首先運行,而不是爲什麼。我有我的推特部分的機器人工作之前,但如果我從網絡抓取數據刮我不能在推文後運行。爲什麼這個功能在Node.JS中不會首先加載?
下面的代碼:
console.log("The Bot Is Starting To Work.");
var sp = require('scrapejs').init({
cc: 2, // up to 2 concurrent requests
delay: 5 * 1 // delay .05 seconds before each request
});
sp.load('https://ca.finance.yahoo.com/q?s=CADUSD=X')
.then(function ($, res) {
//$.q("//h3[@class='r']/a").forEach(function(node){ //Adding the HTML Tags to filter the data
console.log("Scraping The Web...");
$.q("//span[@class='time_rtq_ticker']/span").forEach(function (node) {
var res = {
title: node.textContent //Always returns { title: 'NUMBER'} (When Working)
}
console.log(res);
return res;
})
})
.fail(function (err) {
console.log(err);
})
console.log("Why does this part load first??"); // This part comes first before the function above
繼承人的輸出是什麼樣子:https://imgur.com/YJD2FUW
我得到了它,爲了工作,現在用setTimeout函數現在,我會在一個更好的解決辦法工作,我整理出所有的其他工作後。謝謝! – CorgJR
@CorgJR - 通常當你想在其他函數之後發生某些事情時,可以將它放在函數的回調函數中。超時的問題在於你在那裏使用了一些任意的時間 - 在某些情況下,你會等待太久,在其他時間可能不夠用 –