在node.js中用python shell 呼叫 python

講者:Arashi

日期:2020/05/03

X

Outline

  • 前置作業
  • 實作練習
  • 進階-結合pyhton  crawler

前置作業

  1. 安裝 Node.js
  2. 安裝 Python

程式與軟體

建立環境

npm init -y
npm install express
npm install python-shell

    node-and-python
    ├───── package.json
    ├───── package-lock.json

    └──── node_modules

實作練習

const express = require('express')
const app = express()
let { PythonShell } = require('python-shell')

app.listen(3000, () => {
  console.log('server running on port 3000')
})

app.get('/call/python', pythonProcess)

app-call-python-shell.js

function pythonProcess(req, res) {
  let options = {
    args:
      [
        req.query.name,
        req.query.from
      ]
  }

  PythonShell.run('./process.py', options, (err, data) => {
    if (err) res.send(err)
    const parsedString = JSON.parse(data)
    console.log(`name: ${parsedString.Name}, from: ${parsedString.From}`)
    res.json(parsedString)
  })
}

app-call-python-shell.js

import sys
import json

result = {
  'Name': sys.argv[1],
  'From': sys.argv[2]
}

json = json.dumps(result)

print(str(json))
sys.stdout.flush()

process.py

成果

node app-call-python-shell.js

進階-結合pyhton  crawler

import requests
import re
import sys
import json
from bs4 import BeautifulSoup

def GetNewsContent(WebAddr):
	NewsBody = ""
	url_web = WebAddr
	resp_web = requests.get(url_web)
	
	soup_web = BeautifulSoup(resp_web.text, 'html.parser')
	text_div = soup_web.find_all('div', 'sc-6yh43a-0 ldhHIc')
	for j in text_div:
		context = j.find_all("span")
		for k in context:
			NewsBody = NewsBody +  k.get_text()
	return NewsBody

url = 'https://www.dcard.tw/f'
resp = requests.get(url)

soup = BeautifulSoup(resp.text, 'html.parser')
title_div = soup.find_all('h2', 'sc-1v1d5rx-2 kZjhSU', limit=1)

for i in title_div:
	a_tag = i.find('a')
	title = a_tag.find('span').get_text()
	web = "https://www.dcard.tw"+a_tag.get('href')
	word = GetNewsContent(web)

 Dcrad 爬蟲

with_Dcard.py

result = {
	'Name': word[0:10],
	'From': word[10:20]
}

json = json.dumps(result)

print(str(json)) #印出所抓到的資料
sys.stdout.flush()

把抓到的資料放入result

with_Dcard.py

function pythonProcess(req, res) {
  let options = {
    args:
      [
        req.query.name,
        req.query.from
      ]
  }
  // 修改 process.py 成 with_Dcard.py
  PythonShell.run('./with_Dcard.py', options, (err, data) => {
    if (err) res.send(err)
    const parsedString = JSON.parse(data)
    console.log(`name: ${parsedString.Name}, from: ${parsedString.From}`)
    res.json(parsedString)
  })
}

更改 app-call-python-shell.js 呼叫的 python 檔案

成果

node app-call-python-shell.js

Reference

THANKS FOR LISTENING

HP:50/100

在node.js中用python shell 呼叫 python

By arashi

在node.js中用python shell 呼叫 python

  • 143