因此,我是Web爬取的新手,我正在尝试为Discord机器人发出命令,以使用npm软件包“ Request”和“ Cheerio”使用Steam ID查找Steam帐户。测试完命令后,我意识到使用的初始URL a(https://steamcommunity.com/id/)不适用于每个人的个人资料。我发现有2个URL用于使用Steam ID查找Steam帐户。另一个是https://steamcommunity.com/profiles/。因此,我做了一个使我的机器人尝试第一个URL(https://steamcommunity.com/id/)的函数,如果该方法不起作用,它将尝试第二个URL(https://steamcommunity.com/profiles/)以确保查找所有可能的帐户。问题是由于某种原因,我的Discord僵尸程序花了很多时间从URL(用户名,级别等)中加载数据。我的代码有问题吗?还是互联网等其他问题?
else if (command === "steam")
{
let id = args.join("-");
message.channel.send(message.author + " - Please Wait...");
let requestFunc = function(url) {
request(url + id, (error, response, html) => {
if (!error && response.statusCode == 200)
{
const $ = cheerio.load(html);
const username = $('.persona_name').find('.actual_persona_name');
const profilePic = $('.playerAvatarAutoSizeInner').find('img').attr('src');
const level = $('.persona_level').find('span');
const friendCount = $('.profile_friend_links.profile_count_link_preview_ctn.responsive_groupfriends_element').find('.profile_count_link_total').text().replace(/\s\s+/g, '');
const badgeCount = $('.profile_badges').find('.profile_count_link_total').text().replace(/\s\s+/g, '');
if (username.html() != null || username.text() != '')
{
console.log('[SUCCESS] - Someone is looking up a Steam profile!');
if (level.html() != null || level.text() != '' || friendCount != '' || badgeCount != '')
{
message.channel.send({embed: {
title: "**STEAM PROFILE**",
color: 0x7f7bff,
image: {
url: profilePic
},
fields: [{
name: "Username: " + username.text() + "\nLevel: " + level.text() + "\nNumber of Friends: " + friendCount + "\nNumber of Badges: " + badgeCount,
value: "This account is Public!"
}]
}});
return;
}
else if (username.html() != null || username.text() != '' || level.html() == null || level.text() == '' || friendCount == '' || badgeCount == '')
{
message.channel.send({embed: {
title: "**STEAM PROFILE**",
color: 0x7f7bff,
image: {
url: profilePic
},
fields: [{
name: "Username: " + username.text() + "\nLevel: `PRIVATE`\nNumber of Friends: `PRIVATE`\nNumber of Badges: `PRIVATE`",
value: "This account is Private."
}]
}});
return;
}
}
else if (username.html() == null || username.text() == '')
{
requestFunc('https://steamcommunity.com/profiles/');
return;
}
}
});
}
requestFunc('https://steamcommunity.com/id/');
return;
}