我遇到了一个奇怪的问题,因为python终端正在抛出一个AttributeError: 'NoneType' object has no attribute 'find',但是在调试控制台中没有接收到任何错误,应用程序按照应有的方式工作,没有任何问题。python控制台中的错误让我抓狂,我想了解发生了什么。
我的应用程序的布局如下。我呈现一个主页,它通过html表单从用户那里检索输入,并接受这个值,在本例中是股票代码,然后重定向到函数infoPage,其中股票代码被用来执行一些web抓取。
@app.route('/', methods=['GET', 'POST'])
def stockTickerInput():
if request.method == "POST":
ticker = request.form['ticker'].upper()
return redirect(url_for('infoPage', ticker=ticker))
return render_template('home.html')出于某种原因,python在将flask重定向到infoPage之前正在运行此代码。我知道这一点,因为一旦呈现home.html,我就会在用户输入html表单中的股票代码之前收到错误AttributeError: 'NoneType' object has no attribute 'find'。
@app.route('/<ticker>')
def infoPage(ticker):
# scrapes stock chart/company info from finviz
def stockInfo():
url = 'https://finviz.com/quote.ashx?t=' + ticker
html = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(html.text, 'lxml')
# finds news table within finviz website
stock_website = soup.find('table', class_="fullview-title")
company_name = stock_website.find('a', class_='tab-link').text
return company_name
# functions to be passed as variables in jinja2 template
info = stockInfo()
return render_template('stockInfo.html', ticker=ticker, info=info)回溯:
Traceback (most recent call last):
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\JGrov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\JGrov\Google Drive\GitHub\flask_stockinfo\app.py", line 149, in infoPage
info = stockInfo(ticker)
File "C:\Users\JGrov\Google Drive\GitHub\flask_stockinfo\app.py", line 42, in stockInfo
company_name = stock_website.find('a', class_='tab-link').text
AttributeError: 'NoneType' object has no attribute 'find'我将非常感谢任何关于为什么会发生这种情况的见解。
发布于 2020-07-25 15:30:25
这部分:
if result is None: # if result is none means ticker exists
return redirect(url_for('infoPage', ticker=ticker))执行代码是因为result从这一行中获得一个值为None:
result = match.find('h4').text因此,在return redirect(url_for('infoPage', ticker=ticker))运行之前就会触发return render_template('stockInfo.html', ticker=ticker, info=info)。
https://stackoverflow.com/questions/63089297
复制相似问题