[英]Can I use crawled from Node.js in javaScript?
I'm new to javaScript and trying to crawl a website with node.js. 我是javaScript的新手,正在尝试使用node.js爬行网站。 I could check the data in console log, but want to use the data in another javaScript file.
我可以检查控制台日志中的数据,但想使用另一个javaScript文件中的数据。 How can I fetch the data?
如何获取数据?
The problem is I've never used node.js. 问题是我从未使用过node.js。 I do javaScript so I know how to write the code, but I don't know how the back-end or server works.
我使用javaScript,所以我知道如何编写代码,但不知道后端或服务器的工作方式。
I've tried to open it in my local host but the node method (eg, require()) didn't work. 我试图在本地主机中打开它,但是节点方法(例如require())不起作用。 I found out it's because node doesn't work in browser.(See? very new to js)
我发现这是因为节点在浏览器中不起作用。
Should I use bundler or something? 我应该使用捆绑器之类的东西吗?
The steps I thought were, 我认为的步骤是
Here is the crawling code file. 这是爬网代码文件。
const axios = require("axios");
const cheerio = require("cheerio");
const log = console.log;
const getHtml = async () => {
try {
return await axios.get(URL);
} catch (error) {
console.error(error);
}
};
getHtml()
.then(html => {
let ulList = [];
const $ = cheerio.load(html.data);
const $bodyList = $("div.info-timetable ul").children("li");
$bodyList.each(function(i, elem) {
ulList[i] = {
screen: $(this).find('a').attr('data-screenname'),
time: $(this).find('a').attr('data-playstarttime')
};
});
const data = ulList.filter(n => n.time);
return data;
})
.then(res => log(res));
Could you please explain what steps should I take? 您能解释一下我应该采取什么步骤吗?
Also, it would be great if I can get understood WHY the steps are needed. 另外,如果我能理解为什么需要这些步骤,那将是很好的。
Thanks alot! 非常感谢!
you can try writing your data to a JSON file and proceed, that's one way, then you can use the data as an object in any js file 您可以尝试将数据写入JSON文件并继续进行,这是一种方法,然后您可以将数据用作任何js文件中的对象
const appendFile = (file, contents) =>
new Promise((resolve, reject) => {
fs.appendFile(
file,
contents,
'utf8',
err => (err ? reject(err) : resolve()),
);
});
getHtml()
.then(html => {
let ulList = [];
const $ = cheerio.load(html.data);
const $bodyList = $("div.info-timetable ul").children("li");
$bodyList.each(function(i, elem) {
ulList[i] = {
screen: $(this).find('a').attr('data-screenname'),
time: $(this).find('a').attr('data-playstarttime')
};
});
const data = ulList.filter(n => n.time);
return data;
})
.then(res => {
return appendFile('./data.json',res.toString())
}))
.then(done => {log('updated data json')});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.