汉上
汉上
  • 发布:2024-02-29 23:59
  • 更新:2024-03-01 00:05
  • 阅读:102

写了一个爬网站的云函数有问题

分类:HBuilderX

使用superagent和cheerio写了一个爬网站的云函数有问题,第一次运行是好的,刷新再获取整个hbuild就卡住,然后无法再调用此云函数,代码热更新也失效。

2024-02-29 23:59 负责人:无 分享
已邀请:
汉上

汉上 (作者)

'use strict';
const superagent = require('superagent')
const cheerio = require('cheerio')
const db = uniCloud.database();

function parseData(page) {
const list = [];
const $ = cheerio.load(page)
$('.xxxx').each((index, item) => {
const obj = {
img: $(item).find('img').prop('src'),
name: $(item).find('.name').text()
}
list.push(obj)
})
return list
}

function getList(params) {
return new Promise((resolve, reject) => {
superagent
.get(https://xxxxx/${params}/)
.end(async (err, data) => {
if (err) {
reject(null)
throw new Error('获取失败')
}
resolve(parseData(data.text))
})
})

}

exports.main = async (event, context) => {

let zmdId = await db.collection('related').where({  
        id: event.id  
    }).get()  
    if (zmdId.data.length) {  
        zmdId = zmdId.data[0].zdm_id;  
    } else {  
        throw new Error('暂无列表')  
    }  
return await getList(zmdId);  

};

要回复问题请先登录注册