[英]Please see my problem, believe me it is easy to solve
我試圖在 spawn 子進程中實現 async 和 await 。 但它沒有奏效。 請看這個
預期 output
*************
http://www.stevecostellolaw.com/
*************
http://www.stevecostellolaw.com/personal-injury.html
http://www.stevecostellolaw.com/personal-injury.html
*************
http://www.stevecostellolaw.com/#
http://www.stevecostellolaw.com/#
*************
http://www.stevecostellolaw.com/home.html
http://www.stevecostellolaw.com/home.html
*************
http://www.stevecostellolaw.com/about-us.html
http://www.stevecostellolaw.com/about-us.html
*************
http://www.stevecostellolaw.com/
http://www.stevecostellolaw.com/
*************
Becoz 每次生成子時發現await
它將 go 返回 python 腳本並打印*************
它然后打印 URL。 此處忽略相同 url 的 2 次打印。
我得到的 Output
C:\Users\ASUS\Desktop\searchermc>node app.js
server running on port 3000
DevTools listening on ws://127.0.0.1:52966/devtools/browser/933c20c7-e295-4d84-a4b8-eeb5888ecbbf
[3020:120:0402/105304.190:ERROR:device_event_log_impl.cc(214)] [10:53:04.188] USB: usb_device_handle_win.cc:1056 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[3020:120:0402/105304.190:ERROR:device_event_log_impl.cc(214)] [10:53:04.189] USB: usb_device_handle_win.cc:1056 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
*************
http://www.stevecostellolaw.com/
http://www.stevecostellolaw.com/personal-injury.html
http://www.stevecostellolaw.com/personal-injury.html
http://www.stevecostellolaw.com/#
http://www.stevecostellolaw.com/#
http://www.stevecostellolaw.com/home.html
http://www.stevecostellolaw.com/home.html
http://www.stevecostellolaw.com/about-us.html
http://www.stevecostellolaw.com/about-us.html
http://www.stevecostellolaw.com/
http://www.stevecostellolaw.com/
*************
請參閱下面的app.js
代碼
// form submit request
app.post('/formsubmit', function(req, res){
csvData = req.files.csvfile.data.toString('utf8');
filteredArray = cleanArray(csvData.split(/\r?\n/))
csvData = get_array_string(filteredArray)
csvData = csvData.trim()
var keywords = req.body.keywords
keywords = keywords.trim()
// Send request to python script
var spawn = require('child_process').spawn;
var process = spawn('python', ["./webextraction.py", csvData, keywords, req.body.full_search])
var outarr = []
// process.stdout.on('data', (data) => {
// console.log(`stdout: ${data}`);
// });
process.stdout.on('data', async function(data){
console.log("\n ************* ")
console.log(data.toString().trim())
await outarr.push(data.toString().trim())
console.log("\n ************* ")
});
});
Python function 在 if 條件匹配時發送 URL
# Function for searching keyword start
def search_keyword(href, search_key):
extension_list = ['mp3', 'jpg', 'exe', 'jpeg', 'png', 'pdf', 'vcf']
if(href.split('.')[-1] not in extension_list):
try:
content = selenium_calling(href)
soup = BeautifulSoup(content,'html.parser')
search_string = re.sub("\s+"," ", soup.body.text)
search_string = search_string.lower()
res = [ele for ele in search_key if(ele.lower() in search_string)]
outstr = getstring(res)
outstr = outstr.lstrip(", ")
if(len(res) > 0):
print(href)
found_results.append(href)
href_key_dict[href] = outstr
return 1
else:
notfound_results.append(href)
except Exception as err:
pass
我想做這一切是因為 python 腳本需要更多時間來執行,因此每次都會出現超時錯誤,所以我想在我的 nodejs 腳本中獲得 python 腳本的中間輸出。 您可以在下圖中看到錯誤。
我不確定我是否完全理解你想要做什么,但我會試一試,因為你似乎已經多次問過這個問題(這通常不是一個好主意)。 我相信你的問題不夠明確 - 如果你能澄清你的最終目標是什么(即你希望它如何表現?)
我想你在這里提到了兩個不同的問題。 第一個是您希望在腳本返回的每條單獨的數據之前放置一個新的“******”行。 這是不能依賴的——查看這個問題的答案以獲得更多細節: order of process.stdout.on( 'data', ... ) and process.stderr.on( 'data', . ..) . 數據將以塊的形式傳遞給您的標准輸出處理程序,而不是逐行傳遞,並且可以一次提供任意數量的數據,具體取決於 pipe 中當前的數據量。
我最困惑的部分是您的措辭“在我的 nodejs 腳本中獲取 python 腳本的中間輸出”。 不一定有任何“即時”數據 - 您不能依賴進程的 stdout 處理程序在任何特定時間傳入的數據,它會以 Python 腳本本身及其運行的進程確定的速度向您提供數據。話雖如此,聽起來您的主要問題是您的 POST 發生超時。 你永遠不會結束你的請求——這就是你得到超時的原因。 我將假設您想要等待第一塊數據 - 無論它包含多少行 - 在發送回響應之前。 在這種情況下,您需要添加 res.send,如下所示:
// form submit request
app.post('/formsubmit', function(req, res){
csvData = req.files.csvfile.data.toString('utf8');
filteredArray = cleanArray(csvData.split(/\r?\n/))
csvData = get_array_string(filteredArray)
csvData = csvData.trim()
var keywords = req.body.keywords
keywords = keywords.trim()
// Send request to python script
var spawn = require('child_process').spawn;
var process = spawn('python', ["./webextraction.py", csvData, keywords, req.body.full_search])
var outarr = []
// process.stdout.on('data', (data) => {
// console.log(`stdout: ${data}`);
// });
// Keep track of whether we've already ended the request
let responseSent = false;
process.stdout.on('data', async function(data){
console.log("\n ************* ")
console.log(data.toString().trim())
outarr.push(data.toString().trim())
console.log("\n ************* ")
// If the request hasn't already been ended, send back the current output from the script
// and end the request
if (!responseSent) {
responseSent = true;
res.send(outarr);
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.