使用 Node.js 中的 PythonShell 运行Python脚本
如今,Node.js 是后端开发领域对全球开发人员最有吸引力的技术。如果有人希望使用诸如使用Python模块的 Web Scraping 之类的东西或运行一些具有某些机器学习算法的Python脚本,那么需要知道如何将这两者结合起来。
我们将通过 leetcode 的网页抓取来获取用户的一些数据。那么,让我们开始吧。
现在,首先设置 Node.js 服务器代码。
Javascript
//Import express.js module and create its variable.
const express=require('express');
const app=express();
//Import PythonShell module.
const {PythonShell} =require('python-shell');
//Router to handle the incoming request.
app.get("/", (req, res, next)=>{
//Here are the option object in which arguments can be passed for the python_test.js.
let options = {
mode: 'text',
pythonOptions: ['-u'], // get print results in real-time
scriptPath: 'path/to/my/scripts', //If you are having python_test.py script in same folder, then it's optional.
args: ['shubhamk314'] //An argument which can be accessed in the script using sys.argv[1]
};
PythonShell.run('python_test.py', options, function (err, result){
if (err) throw err;
// result is an array consisting of messages collected
//during execution of script.
console.log('result: ', result.toString());
res.send(result.toString())
});
});
//Creates the server on default port 8000 and can be accessed through localhost:8000
const port=8000;
app.listen(port, ()=>console.log(`Server connected to ${port}`));
Python3
# code
import sys
import requests
from bs4 import BeautifulSoup
from csv import writer
# bs4 module for web scraping and requests for making HTTPS requests using Python.
response = requests.get('https://leetcode.com / shubhamk314')
soup = BeautifulSoup(response.text, 'html.parser')
main_content = soup.select(
'# base_content>div>div>div.col-sm-5.col-md-4>div:nth-child(3)>ul')
list_items = main_content[0].select('li')
items = ['Solved Question', 'Accepted Submission', 'Acceptance Rate']
n = 0
# It will create csv files named progress.csv in root folder once this is script is called.
with open('progress.csv', 'w') as csv_file:
csv_writer = writer(csv_file)
headers = ['Name', 'Score']
csv_writer.writerow(headers)
while(n < 3):
name = items[n]
score = list_items[n].find('span').get_text().strip()
csv_writer.writerow([name, score])
n = n + 1
print("csv file created for leetcode")
现在, Python脚本。 (确保您已经安装了 bs4 模块和 csv 模块。)
Python3
# code
import sys
import requests
from bs4 import BeautifulSoup
from csv import writer
# bs4 module for web scraping and requests for making HTTPS requests using Python.
response = requests.get('https://leetcode.com / shubhamk314')
soup = BeautifulSoup(response.text, 'html.parser')
main_content = soup.select(
'# base_content>div>div>div.col-sm-5.col-md-4>div:nth-child(3)>ul')
list_items = main_content[0].select('li')
items = ['Solved Question', 'Accepted Submission', 'Acceptance Rate']
n = 0
# It will create csv files named progress.csv in root folder once this is script is called.
with open('progress.csv', 'w') as csv_file:
csv_writer = writer(csv_file)
headers = ['Name', 'Score']
csv_writer.writerow(headers)
while(n < 3):
name = items[n]
score = list_items[n].find('span').get_text().strip()
csv_writer.writerow([name, score])
n = n + 1
print("csv file created for leetcode")
保存这两个文件后,从其根文件夹运行以下命令:
node test.js
现在,在浏览器中通过 localhost:8000 发送请求。
如果一切顺利,那么输出将是:
消息:为 leetcode 创建的 csv 文件。
结论
这是如何使用 Node.js 运行Python脚本的简单实现,这在您有一堆 Node.js 应用程序并且想要运行简单的Python脚本的情况下很有用。如果您想了解更多关于 PythonShell 模块的信息,请浏览给定的链接。
链接:https://github.com/extrabacon/python-shell