Python 如何创建一个node.js脚本,该脚本可以单击启动该服务器的外部网站上的按钮
我正在尝试构建一个discord机器人,它将接受启动Minecraft服务器的命令,并单击单独网站上的按钮来启动所述服务器。单击按钮的代码从python文件运行,但我的bot是在node.js中生成的。是否有一种方法可以创建允许node.js使用python文件的东西?我已经开始使用pythonshell,但无法让它工作 Node.js代码:Python 如何创建一个node.js脚本,该脚本可以单击启动该服务器的外部网站上的按钮,python,node.js,webserver,Python,Node.js,Webserver,我正在尝试构建一个discord机器人,它将接受启动Minecraft服务器的命令,并单击单独网站上的按钮来启动所述服务器。单击按钮的代码从python文件运行,但我的bot是在node.js中生成的。是否有一种方法可以创建允许node.js使用python文件的东西?我已经开始使用pythonshell,但无法让它工作 Node.js代码: const { prefix } = require('config.json'); const Discord = require('discord.j
const { prefix } = require('config.json');
const Discord = require('discord.js');
const client = new Discord.Client();
const token = process.env.DISCORD_BOT_SECRET;
const aternosusername = process.env.ATERNOSUSERNAME;
const aternospassword = process.env.ATERNOSPASSWORD;
const keep_alive = require('./keep_alive.js')
var sleep = require('sleep');
//var PythonShell = require('python-shell');
const puppeteer = require('puppeteer')
client.on('ready', () => {
console.log("I'm in");
console.log(client.user.username);
});
client.on('message', msg => {
if (msg.content === 'Start Server'){
//PythonShell.run('serverstart.py', options, ClickStart() (err, results))
msg.channel.send('Starting Please Wait.........');
sleep.sleep(2);
msg.channel.send('Server is now starting. This will take 5 to 8 mins.');
ClickStart();
}
});
async function ClickStart () {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.waitFor(1000);
await page.goto('https://aternos.org/go/');
await page.authentication(credidentials);
await page.click('#user');
await page.keyboard.type('World', {delay: 100});
await page.click('#password');
await page.click('#login');
console.log('Loging In');
await page.waitForNavigation();
await page.click('#start');
console.log('Starting Server');
}
client.login(token);
您可以使用npm模块Puppeter与ui交互 步骤:
npm install puppeteer --save
然后在nodejs代码中执行如下操作
const puppeteer = require('puppeteer')
(async () => {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.waitFor(1000); //callibrate as per your need
await page.goto('www.yourwebsite.com');
await page.click('#yourButtonId');
await page.waitForNavigation();
})()
查看节点模块了解更多使用指南。您可以使用npm模块Puppeter与ui交互 步骤:
npm install puppeteer --save
然后在nodejs代码中执行如下操作
const puppeteer = require('puppeteer')
(async () => {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.waitFor(1000); //callibrate as per your need
await page.goto('www.yourwebsite.com');
await page.click('#yourButtonId');
await page.waitForNavigation();
})()
查看节点模块以了解更多的使用指南。更新了我的答案,因为cheerio在这种情况下没有帮助。好的,我已经更改了代码(张贴在上面),它可以工作。有没有一种方法可以使用此代码在网站的文本框中写入文本?是的,您可以。尝试以下操作:::wait page.type('input[name=pickupAgingComment],'test comment',{delay:20})更新了我的答案,因为cheerio在这种情况下没有帮助。好的,我已经更改了代码(发布在上面),它可以正常工作。有没有一种方法可以使用此代码在网站的文本框中写入文本?是的,您可以。尝试以下操作:::wait page.type('input[name=pickupagingcoment],'testcomment',{delay:20})