Cheerio 没有通过 Discord bot 使用 Node.js 阅读许多网站

问题描述

我对编程非常陌生,只有三个学期的 C++,但想开始制作一些项目,所以我考虑制作一个 discord 机器人,它可以为我提供产品的库存更新。机器人运行后,我开始从单独的教程中使用 javascript 和cheerio。但是,在设置我对要阅读的网页的请求时,许多站点在被告知 console.log(html); 时不会返回任何内容。我发现唯一有效的网站是目标主页,我尝试过的其他网站都没有,包括主页和产品页面。查看特定目标产品页面时也不会返回任何内容,例如网络摄像头。有谁知道这是为什么?

我的功能代码

    const request = require('request');
const cheerio = require('cheerio');

module.exports = {
    name: 'get',description:'Looks for item',execute(message,args){
        console.log('in get');
        request('https://www.bestbuy.com/',(error,response,html) => {
            if(!error && response.statusCode == 200){
                console.log(html);
                const $ = cheerio.load(html);


            }
        });
    }
}

还有我的主文件,对于不和谐机器人来说更是如此

const discord = require('discord.js');
const client = new discord.Client();
const request = require('request');
const cheerio = require('cheerio');
const prefix = '!';
 
const fs = require('fs');
 
client.commands = new discord.Collection();
 
const commandFiles = fs.readdirsync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);
 
    client.commands.set(command.name,command);
}
 
 
client.once('ready',() => {
    console.log('Inventory bot is online.');
});
 
client.on('message',message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;
 
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
 
    if(command === 'ping'){
        client.commands.get('ping').execute(message,args);
    } else if(command === 'get'){
        client.commands.get('get').execute(message,args);
    }
});

我的教程是此播放列表中的前三个视频,用于机器人和设置命令,它们都可以独立运行。 https://www.youtube.com/playlist?list=PLbbLC0BLaGjpyzN1rg-gK4dUqbn8eJQq4

在遇到这个障碍之前,我一直在使用 Cheerio 教程:https://www.youtube.com/watch?v=LoziivfAAjE。同样,这是我遇到的唯一问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)