简体   繁体   English

将 Python 脚本连接到 Nodejs

[英]Connect Python Script to Nodejs

Complete new to Node.js.全新的 Node.js。 Recently I was provided with a task to web scrape a website with python and connect the python script to Node.js using python-shell package in Node.js.最近我接到了一个任务,用 python 抓取一个网站,并使用 Node.js 中的 python-shell 包将 python 脚本连接到 Node.js。 I'm done with the scraping part but not having any prior knowledge of Node.js.我已经完成了抓取部分,但对 Node.js 没有任何先验知识。 Can you please guide me on how to approach this problem?你能指导我如何解决这个问题吗?

Since you must use python-shell package and assuming that your source file is my_file.py , you can simply read the documentation of the npm package and in particular this example:由于你必须使用python-shell包并假设你的源文件是my_file.py ,你可以简单地阅读npm 包的文档,特别是这个例子:

import {PythonShell} from 'python-shell';
let pyshell = new PythonShell('my_script.py'); // 1
 
pyshell.on('message', function (message) {
  // received a message sent from the Python script (a simple "print" statement)
  console.log(message); // here there will be logged your pyoutput
}); // 2
 
// end the input stream and allow the process to exit
pyshell.end(function (err,code,signal) {
  if (err) throw err;
  console.log('The exit code was: ' + code);
  console.log('The exit signal was: ' + signal);
  console.log('finished');
  console.log('finished');
}); // 3

What this script does:这个脚本的作用:

  1. launches the python script without arguments不带参数启动python脚本
  2. gets every write on the stdoutput of your pyscript and logs it into nodejs stdout.获取对 pyscript 的 stdoutput 的每次写入并将其记录到 nodejs stdout 中。
  3. closes the python script reporting some optional info关闭报告一些可选信息的 python 脚本

If your scripts logs the webscrape into the stdout this should work fine.如果您的脚本将 webscrape 记录到标准输出中,这应该可以正常工作。

import sys 
# Takes first name and last name via command 
# line arguments and then display them 
print("Output from Python") 
print("First name: " + sys.argv[1]) 
print("Last name: " + sys.argv[2]) 

# save the script as hello.py 

// import express JS module into app 
// and creates its variable. 
var express = require('express'); 
var app = express(); 

// Creates a server which runs on port 3000 and 
// can be accessed through localhost:3000 
app.listen(3000, function() { 
    console.log('server running on port 3000'); 
} ) 

// Function callName() is executed whenever 
// url is of the form localhost:3000/name 
app.get('/name', callName); 

function callName(req, res) { 

    // Use child_process.spawn method from 
    // child_process module and assign it 
    // to variable spawn 
    var spawn = require("child_process").spawn; 

    // Parameters passed in spawn - 
    // 1. type_of_script 
    // 2. list containing Path of the script 
    // and arguments for the script 

    // E.g : http://localhost:3000/name?firstname=Mike&lastname=Will 
    // so, first name = Mike and last name = Will 
    var process = spawn('python',["./hello.py", 
                            req.query.firstname, 
                        req.query.lastname] ); 

    // Takes stdout data from script which executed 
    // with arguments and send this data to res object 
    process.stdout.on('data', function(data) { 
        res.send(data.toString()); 
    } ) 
} 

// save code as start.js // 将代码保存为 start.js

After saving the Python script and server script code, run the code from its source folder by following command :保存 Python 脚本和服务器脚本代码后,通过以下命令从其源文件夹运行代码:

node start.js

Access the application through link :通过链接访问应用程序:

localhost:3000/name?firstname="Enter first name"&lastname="Enter last name"

For e g.例如。 : localhost:3000/name?firstname=Ram&lastname=Sharma : localhost:3000/name?firstname=Ram&lastname=Sharma

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM