[英]Run scrapy from Flask application
我有一個爬蟲,我想每次有人去鏈接時運行。 由於所有其他模塊都在Flask中,我被告知要在Flask中構建它。 我已經在虛擬環境中安裝了scrapy和selenium,並在root用戶機器上全局安裝了scrapy和selenium。
當我通過終端運行爬蟲時,一切正常。 當我啟動Flask應用程序並訪問瀏覽器中的xx.xx.xx.xx:8080/whats
時,這也可以正常運行我的抓取工具並獲取該文件。 但是一旦我上線,以便任何人一旦進入鏈接,它就會在瀏覽器中給出內部錯誤。
為了運行crawler,我們必須在終端中鍵入“scrapy crawl whateverthespidernameis”。 我是使用Python的os
模塊完成的。
這是我的燒瓶代碼:
import sys
from flask import request, jsonify, render_template, url_for, redirect, session, abort,render_template_string,send_file,send_from_directory
from flask import *
#from application1 import *
from main import *
from test123 import *
import os
app = Flask(__name__)
filename = ''
app = Flask(__name__)
@app.route('/whats')
def whats():
os.getcwd()
os.chdir("/var/www/myapp/whats")
//cmd = "scrapy crawl whats"
cmd = "sudo scrapy crawl whats"
os.system(cmd)
return send_file("/var/www/myapp/staticcsv/whats.csv", as_attachment =True)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080,debug=True)
這是我在實時鏈接中運行時記錄在日志文件中的錯誤:
sh: 1: scrapy: not found**
這是我在命令中使用sudo時記錄在日志文件中的錯誤(變量cmd
):
sudo: no tty present and no askpass program specified**
我正在使用uwsgi和nginx。
如何運行此爬蟲,以便當任何人轉到“xx.xx.xx.xx / whats”時,爬蟲運行並返回csv文件?
當你使用sudo
shell時,這將啟動tty上的密碼 - 它特別不會讀取此信息的標准輸入。 由於flask
和其他Web應用程序通常從終端分離,因此sudo
無法請求密碼,因此它會查找可以提供密碼的程序。 您可以在此答案中找到有關此主題的更多信息。
您沒有找到scrapy
的原因很可能是因為您在測試中使用的交互式shell與運行flask
的過程之間的$PATH
存在差異。 解決這個問題的最簡單方法是在命令中提供scrapy
程序的完整路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.