【发布时间】:2021-06-17 16:01:30
【问题描述】:
我正在创建一个离子应用程序。我正在使用 Cheerio 和 Node.js 来抓取网站。我无法将整个数组返回到 localhost,它只返回第一个对象。我怎样才能返回整个数组?
这是抓取代码(scraper.js)
const request = require('request')
const cheerio = require('cheerio');
request('https://mywebsite', (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html)
const art = $('article').each((i, el) => {
const title = $(el)
.find('.entry-title.mh-posts-grid-title')
.text();
const link = $(el)
.find('a')
.attr('href');
const image = $(el)
.find('.mh-thumb-icon.mh-thumb-icon-small-mobile')
.text()
const Arra = { title: [title], link: [link], image: [image] }
exports.Arra = Arra
这是我导出数据的代码(document.js)
const express = require("express");
const ok = require('./ok')
const app = express();
const porta = '8000'
app.get('/', (req, res) => {
res.send(ok.Arra);
})
app.listen(porta, () => {
console.log(`Server ex ${porta}`);
});
【问题讨论】:
-
查看数据样本也会有所帮助。您至少还缺少 scraper.js 代码中的几个结束标记。
-
只返回第一个数组
-
缺少哪些标签?
-
1) 如果您使用 console.log('$ equals', $) 控制台中显示的内容。那是样本数据。 2) 在提供的示例中,您缺少请求行 { 、if 行 { 和 const art 行 { 的结束标记
-
你能解释一下吗
标签: javascript node.js arrays ionic-framework cheerio