前提・実現したいこと
flaskを使いスクレイピングしたデータフレームをhtmlで選択したデータを表示させたい。
手順
1.野球のデータをpandasを使ってスクレイピングする。
2.htmlで選択肢を配置。
3.pandasで作ったスクレイピング関数をflaskを使って選択したdataframだけ表示させる。
チームと年代を選択して決定を押してもエラーが出るので直したい。
発生している問題・エラーメッセージ
(base) hatea@hateanoMacBook-Pro baseball data % source /Users/hatea/opt/anaconda3/bin/activate (base) hatea@hateanoMacBook-Pro baseball data % conda activate base (base) hatea@hateanoMacBook-Pro baseball data % /Users/hatea/opt/anaconda3/bin/python "/Users/hatea/Desktop/baseball data/flask/app.py" * Serving Flask app "app" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with fsevents reloader * Debugger is active! * Debugger PIN: 653-563-779 127.0.0.1 - - [22/Jun/2020 21:42:23] "GET / HTTP/1.1" 200 - m 127.0.0.1 - - [22/Jun/2020 21:42:28] "POST / HTTP/1.1" 500 - Traceback (most recent call last): File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__ return self.wsgi_app(environ, start_response) File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app response = self.handle_exception(e) File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception reraise(exc_type, exc_value, tb) File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/Users/hatea/Desktop/baseball data/flask/app.py", line 15, in index players = base_ball.data_load(year,team) File "/Users/hatea/Desktop/baseball data/flask/base_ball.py", line 14, in data_load dfs = pd.io.html.read_html(BASE_URL) File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py", line 1100, in read_html displayed_only=displayed_only, File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py", line 895, in _parse tables = p.parse_tables() File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py", line 213, in parse_tables tables = self._parse_tables(self._build_doc(), self.match, self.attrs) File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py", line 733, in _build_doc raise e File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/pandas/io/html.py", line 714, in _build_doc with urlopen(self.io) as f: File "/Users/hatea/opt/anaconda3/lib/python3.7/site-packages/pandas/io/common.py", line 141, in urlopen return urllib.request.urlopen(*args, **kwargs) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 531, in open response = meth(req, response) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 563, in error result = self._call_chain(*args) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 755, in http_error_302 return self.parent.open(new, timeout=req.timeout) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 531, in open response = meth(req, response) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 569, in error return self._call_chain(*args) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/Users/hatea/opt/anaconda3/lib/python3.7/urllib/request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 404: Not Found 127.0.0.1 - - [22/Jun/2020 21:42:28] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [22/Jun/2020 21:42:28] "GET /?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 - 127.0.0.1 - - [22/Jun/2020 21:42:28] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [22/Jun/2020 21:42:28] "GET /?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 - 127.0.0.1 - - [22/Jun/2020 21:42:28] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
該当のソースコード
python
1base_ball.py 2from IPython import get_ipython 3import random 4import matplotlib.pyplot as plt 5import numpy as np 6import pandas as pd 7import sys 8#野手データ取得 9def data_load(year,team): 10 BASE_URL = ("http://npb.jp/bis/{}/stats/idb1_{}.html".format(year,team)) 11 dfs = pd.io.html.read_html(BASE_URL) 12#カラムの再設定 13 df = dfs[0][1:]; df.columns=dfs[0].loc[0,:] 14 new_header = df.iloc[0] 15 df = df[1:] 16 df.columns = new_header 17 df2 = df.rename(columns=lambda s: str(s).replace(" ","")) 18 df_i = df2.rename(columns=lambda s: str(s).replace(" ","")) 19 del df_i['nan'] 20 df3 = df_i.set_index('選手') 21 return df3 22
python
1app.py 2import os 3from flask import Flask, render_template,request 4import base_ball 5app = Flask(__name__) 6 7@app.route('/',methods=["GET","POST"]) 8def index(): 9 if request.method == 'POST': 10 year = request.form.get('year','') 11 team = request.form.get('team','') 12 print(year,team) 13 if year == '2019' and team == 'bs': 14 players = base_ball.data_load(year,'b') 15 else: 16 players = base_ball.data_load(year,team) 17 if int(year) >= 2011 and team == 'db': 18 players = base_ball.data_load(year,'yb') 19 else: 20 players = base_ball.data_load(year,team) 21 players = base_ball.data_load(year,team) 22 players_values = players.values.tolist() 23 players_columns = players.columns.tolist() 24 players_index = players.index.tolist() 25 return render_template('index.html', \ 26 players_values = players_values, \ 27 players_columns = players_columns, \ 28 players_index = players_index) 29 return render_template('index.html') 30 31if __name__ == '__main__': 32 app.run(debug=True, host='127.0.0.1',port=5000,threaded=True)
html
1index.html 2 <fieldset> 3 <legend>自チームを選ぼう</legend> 4 <form method="post" action="/"> 5 <label for="year">年代を選択</label> 6 <select id="year"> 7 <option name="year" value="2005">2005年</option> 8 <option name="year" value="2006">2006年</option> 9 <option name="year" value="2007">2007年</option> 10 <option name="year" value="2008">2008年</option> 11 <option name="year" value="2009">2009年</option> 12 <option name="year" value="2010">2010年</option> 13 <option name="year" value="2011">2011年</option> 14 <option name="year" value="2012">2012年</option> 15 <option name="year" value="2013">2013年</option> 16 <option name="year" value="2014">2014年</option> 17 <option name="year" value="2015">2015年</option> 18 <option name="year" value="2016">2016年</option> 19 <option name="year" value="2017">2017年</option> 20 <option name="year" value="2018">2018年</option> 21 <option selected name="year" value="2019">2019年</option> 22 </select> 23 <label><input type="radio" name="team" value="h">ソフトバンク</label> 24 <label><input type="radio" name="team" value="l">西武</label> 25 <label><input type="radio" name="team" value="e">楽天</label> 26 <label><input type="radio" name="team" value="m">ロッテ</label> 27 <label><input type="radio" name="team" value="f">日本ハム</label> 28 <label><input type="radio" name="team" value="bs">オリックス</label> 29 <label><input type="radio" name="team" value="c">広島</label> 30 <label><input type="radio" name="team" value="db">横浜</label> 31 <label><input type="radio" name="team" value="g">巨人</label> 32 <label><input type="radio" name="team" value="t">阪神</label> 33 <label><input type="radio" name="team" value="s">ヤクルト</label> 34 <label><input type="radio" name="team" value="d">中日</label> 35 </fieldset> 36 <div><input type="submit" value="決定"></div> 37 </form> 38 <table> 39 <thead> 40 <tr>{%- for i in players_columns %}<th>{{ i|e }}</th>{%- endfor %}</tr> 41 </thead> 42 <tbody> 43 {%- for i in players_values %} 44 <tr><th>{{ players_index[loop.index0]|e }}</th>{% for j in i %}<td>{{ j|e }}</td>{% endfor %}</tr> 45 {%- endfor %} 46 </tbody> 47 </table> 48 <fieldset>
補足情報(FW/ツールのバージョンなど)
python(3.7)
あなたの回答
tips
プレビュー