在执行我的 Python 脚本时,我刚刚收到以下类型错误:
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/Zach/Dropbox/stock/stk.py", line 31, in stock
url="http://finance.yahoo.com/d/quotes.csv?s="+stock+"&f=snl1"
TypeError: cannot concatenate 'str' and 'function' objects
导致问题的行似乎是url="http://finance.yahoo.com/d/quotes.csv?s="+stock+"&f=snl1"
,特别是变量stock.
经过研究和我从以前的错误中了解到的一点点,我推断原因一定是变量定义的问题(它不是字符串,或者可能没有定义完全);也就是说,我之前在函数中将此变量定义为字符串:
@app.route('/', methods=['GET', 'POST'])
def home_search():
if request.method == 'POST':
stock = request.form['s']
return redirect(url_for('stock'))
return render_template('stk.html')
这是函数,stock
我尝试调用并显示之前定义的值:
@app.route('/stock', methods=['GET', 'POST'])
def stock():
print type(stock)
url="http://finance.yahoo.com/d/quotes.csv?s="+stock+"&f=snl1"
text=urllib2.urlopen(url).read()
return render_template('stock.html')
但是,它的定义与@app.route
我调用它时的不同。Python中的变量不应该像那样存储和调用,还是在加载新页面时变量被清空?如果是这种情况,任何有关解决方法或更好方法的建议将不胜感激。