【发布时间】:2015-08-17 07:05:52
【问题描述】:
我有一个爬虫,我想在每次有人访问链接时运行它。由于所有其他模块都在 Flask 中,我被告知也要在 Flask 中构建它。我已经在虚拟环境和全局机器上安装了scrapy和selenium。
当我通过终端运行爬虫时,一切正常。当我启动 Flask 应用程序并在浏览器中访问 xx.xx.xx.xx:8080/whats 时,它也可以正常工作并运行我的爬虫并获取文件。但是一旦我上线,每当有人访问链接时,它就会在浏览器中给我带来内部错误。
为了运行爬虫,我们必须在终端中输入“scrapy crawlwhatthespidernameis”。我使用 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 文件?
【问题讨论】:
-
你的代码真的有
cmd = "sudo crapy crawl whats"这一行吗(注意调用了一个名为crapy的程序)? -
你正在使用 python os 命令' cmd = "sudo crapy crawl whats" '。我认为应该是' cmd = "sudo scrapy crawl whats" '。下一点是检查你的scrapy PATH,纠正它。它将运行没有任何错误。 :)
-
感谢您指出帖子中的错误,我已经编辑了
-
我在这里回答了类似的问题:stackoverflow.com/questions/36384286/…
标签: python nginx flask scrapy scrapy-spider