如何解决如何在Ejs模板中打印Web抓取的项目?
我正在尝试Web爬网,并且我的后端代码运行正常。我的问题是将数据打印到前端。我使用Ejs模板。当我尝试使用foreach方法打印时,它仅打印最后3次。但是,有很多废品。我该如何解决?
这是我的后端代码:
const axios = require('axios');
const cheerio = require('cheerio');
const _ = require('lodash');
module.exports.getIndex = (req,res) => {
const items = {
names: [],min: [],max: []
};
axios.get('https://www.antalya.bel.tr/BilgiEdin/halden-gunluk-fiyatlar').then(res => {
const $ = cheerio.load(res.data);
$('.market-place-item-name .text-dark').each((index,element) => {
const temp = $(element).text()
_.update(items,'names',function () {
return temp;
})
})
$('.market-place-pricing-wrap .text-dark').each((index,'min',function () {
return temp;
})
})
})
.then(() => {
res.render('home/index.ejs',{
items: items,})
})
}
这是我的前端:
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<% Object.keys(items).forEach(i => { %>
<tr>
<td>
<%= items.names %>
</td>
<td>
<%= items.min %>
</td>
</tr>
<% }) %>
</tbody>
</table>
这是我拍摄的结果的图像: Click to see image
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。