运行Flask刮屑应用程序

2024-04-26 04:30:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个爬虫,我想运行每一个人的链接。因为所有其他模块都在烧瓶里,所以我被告知也要在烧瓶里做这个。我已经在虚拟环境中安装了scrapy和selenium,并在具有root用户的机器上全局安装了scrapy和selenium。在

当我在终端运行爬虫程序时,一切正常。当我启动Flask应用程序并在浏览器中访问xx.xx.xx.xx:8080/whats时,这也很好,它运行我的爬虫程序并获取文件。但一旦我上线,任何时候有人访问链接,浏览器就会出现内部错误。在

为了运行crawler,我们必须在终端中输入“scrapy craw whateverthespidernamis”。我使用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)

这是我运行live link时在日志文件中记录的错误:

^{pr2}$

这是在命令(变量cmd)中使用sudo时在日志文件中记录的错误:

sudo: no tty present and no askpass program specified**

我正在使用uwsgi和nginx。在

我怎么能运行这个爬虫程序,这样当有人去xx.xx.xx.xx/whats“爬虫程序运行并返回csv文件?”?在


Tags: 文件fromimport程序cmdsendappflask
1条回答
网友
1楼 · 发布于 2024-04-26 04:30:11

当您使用sudo时,这个启动的shell将请求tty上的密码-它不会读取该信息的标准输入。由于flask和其他web应用程序通常是独立于终端运行的,sudo无法请求密码,因此它寻找能够提供密码的程序。您可以在this answer中找到有关此主题的详细信息。在

找不到scrapy的原因很可能是因为您在测试中使用的交互式shell与运行flask的进程之间的$PATH存在差异。解决这个问题最简单的方法是在命令中给出scrapy程序的完整路径。在

相关问题 更多 >