问题描述
我正在尝试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 (将#修改为@)