CS50 金融的无效符号,即使我使用了正确的符号
Invalid Symbol for CS50 finance, even when I use the correct symbol
我在获取 CS50 Finance 的股票报价时遇到问题,无论我输入什么股票代码,它 returns 都是“无效代码”的错误,我终生无法弄清楚为什么。我已经包含了我的 application.py"
的相关部分
@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
if request.method == "POST":
quote=lookup(request.form.get("symbol"))
if quote == None:
return apology ("invalid symbol", 400)
return render_template ("quoted.html", quote=quote)
else:
return render_template("quote.html")
以及我的 quote.html(似乎有效)
{% extends "layout.html" %}
{% block title %}
Quote
{% endblock %}
{% block main %}
<form action="/quote" method="post">
<div class= "form-group">
<input autocomplete="off" autofocus class="form-control" name ="symbol" placeholder = "type symbol here" type="text" require/>
</div>
<button class="btn btn-primary" type="submit">Get quote</button>
</form>
{% endblock %}
和我的quoted.html(我从来没有看到,因为我收到了道歉:
{% extends "layout.html" %}
{% block title %}
Quoted
{% endblock %}
{% block main %}
<p> Today, a share of {{ quote ["symbol"] }} will cost you {{ quote ["price"] | usd }}.</p>
{% endblock %}
任何帮助将不胜感激,提前致谢
编辑:这是来自 helpers.py 的查找,我没有编写这段代码,它是为 CS50 class:
提供的
"""Look up quote for symbol."""
# reject symbol if it starts with caret
if symbol.startswith("^"):
return None
# reject symbol if it contains comma
if "," in symbol:
return None
# query Yahoo for quote
#
try:
url = "http://download.finance.yahoo.com/d/quotes.csv?f=snl1&s={}".format(symbol)
webpage = urllib.request.urlopen(url)
datareader = csv.reader(webpage.read().decode("utf-8").splitlines())
row = next(datareader)
except:
return None
# ensure stock exists
try:
price = float(row[2])
except:
return None
# return stock's name (as a str), price (as a float), and (uppercased) symbol (as a str)
return {
"name": row[1],
"price": price,
"symbol": row[0].upper()
}
好的,所以我想 post 一个正式的回答,但我必须在致谢名单中感谢你们。 GAEfan 向我指出了正确的方向,问题可能出在我的 lookup() 中。然后 Barmar 指出用于我的 lookup() 的网站不再使用。我不知道发生了什么,但我从官方 CS50 link 下载了分发代码,也许我遇到了故障或下载错误,但重新下载分发代码,让我获得了最新的lookup() 函数的版本。不打算回答我自己的问题,但想结束这个问题并感谢大家帮助我。
我在获取 CS50 Finance 的股票报价时遇到问题,无论我输入什么股票代码,它 returns 都是“无效代码”的错误,我终生无法弄清楚为什么。我已经包含了我的 application.py"
的相关部分@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
if request.method == "POST":
quote=lookup(request.form.get("symbol"))
if quote == None:
return apology ("invalid symbol", 400)
return render_template ("quoted.html", quote=quote)
else:
return render_template("quote.html")
以及我的 quote.html(似乎有效)
{% extends "layout.html" %}
{% block title %}
Quote
{% endblock %}
{% block main %}
<form action="/quote" method="post">
<div class= "form-group">
<input autocomplete="off" autofocus class="form-control" name ="symbol" placeholder = "type symbol here" type="text" require/>
</div>
<button class="btn btn-primary" type="submit">Get quote</button>
</form>
{% endblock %}
和我的quoted.html(我从来没有看到,因为我收到了道歉:
{% extends "layout.html" %}
{% block title %}
Quoted
{% endblock %}
{% block main %}
<p> Today, a share of {{ quote ["symbol"] }} will cost you {{ quote ["price"] | usd }}.</p>
{% endblock %}
任何帮助将不胜感激,提前致谢
编辑:这是来自 helpers.py 的查找,我没有编写这段代码,它是为 CS50 class:
提供的"""Look up quote for symbol."""
# reject symbol if it starts with caret
if symbol.startswith("^"):
return None
# reject symbol if it contains comma
if "," in symbol:
return None
# query Yahoo for quote
#
try:
url = "http://download.finance.yahoo.com/d/quotes.csv?f=snl1&s={}".format(symbol)
webpage = urllib.request.urlopen(url)
datareader = csv.reader(webpage.read().decode("utf-8").splitlines())
row = next(datareader)
except:
return None
# ensure stock exists
try:
price = float(row[2])
except:
return None
# return stock's name (as a str), price (as a float), and (uppercased) symbol (as a str)
return {
"name": row[1],
"price": price,
"symbol": row[0].upper()
}
好的,所以我想 post 一个正式的回答,但我必须在致谢名单中感谢你们。 GAEfan 向我指出了正确的方向,问题可能出在我的 lookup() 中。然后 Barmar 指出用于我的 lookup() 的网站不再使用。我不知道发生了什么,但我从官方 CS50 link 下载了分发代码,也许我遇到了故障或下载错误,但重新下载分发代码,让我获得了最新的lookup() 函数的版本。不打算回答我自己的问题,但想结束这个问题并感谢大家帮助我。