繁体   English   中英

如何从快速路线中调用我的 web 刮板?

[英]How to call my web scraper from within an express route?

我的 Nodejs 服务器的根目录中有一个 web 刮板,运行 express。 这是一个使用 needle 对 html 发出获取请求的文件,它返回一个数据数组。 然后我有我的 index.js 和我的快速路线,一个简单的路线,比如"/api/scrape"

但是,当我从 go 到那个 url 时,服务器控制台仍然会在刮板中打印出一个 console.log 以表明它正在完成。 但是快递想在等待刮板完成之前继续前进。

在将响应发送回 React 之前,如何等待爬虫完成。

You can try converting your function which calls the web scraper to an async function, and have express call the function with await. 这样,express 将等待返回 promise,而不是继续前进。

我相信这也可以通过将其作为中间件在 express 中实现:

const example_scrape = (req, res, next) => {
... do your scraping
next() //allows express to move onto the next route? not sure the terminology 
}


app.get('/api/scrape_my_site', example_scrape, (req,res) => {
...
}

你可以看看这个项目 - https://github.com/vodolaz095/email-parser-api

它是简单的 Selenium 驱动的 api,它是从 nodejs 应用程序公开的 REST ZDB974238714CA8ACE14D604 中调用的。

代码很简单

这是 expressjs 路由器代码,它实际上调用刮板以响应 POST 请求

https://github.com/vodolaz095/email-parser-api/blob/master/index.js#L21-L53

这是刮板代码

https://github.com/vodolaz095/email-parser-api/blob/master/lib/parser.js

暂无
暂无

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

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