講者:Arashi
日期:2020/05/03
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)
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)
})
}
import sys
import json
result = {
'Name': sys.argv[1],
'From': sys.argv[2]
}
json = json.dumps(result)
print(str(json))
sys.stdout.flush()
node app-call-python-shell.js
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)
with_Dcard.py
result = {
'Name': word[0:10],
'From': word[10:20]
}
json = json.dumps(result)
print(str(json)) #印出所抓到的資料
sys.stdout.flush()
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)
})
}
node app-call-python-shell.js
HP:50/100