概要
自作Linebot用のデータベースとして使うことを念頭に、犬に食べさせてはいけないもののリストを作成しました。
情報は犬に食べさせてはいけない物(138品目) | 迷子ペット.NETを参考にいたしました。
環境
macOS Catalina 10.15.4
python 3.8.0
表の情報をテキストファイルにコピー(手動)
ホームページの表の情報を手動でテキストエディタにコピペします。多分スクレイピングするよりも手動のほうが早いです。
情報の成形
テキストファイルの文字列をpythonで取り込んで、成形し、jsonに保存します。
文字列の取り込みはクリップボード経由で、pyperclipから行い、ipythonで作業しました。ファイル名をつけて保存して、openで読み込んでもよいと思います。
import pyperclip
import json
OUT_FILE = 'botanswer.json'
#クリップボードの読み込み
table = pyperclip.paste().splitlines()
#1行目(食品 生食 加熱)の取得
header = table[0].split()
botanswer = {}
explanation = "〇:少量であれば与えてもいい。\n△:与えすぎに注意してください。\n×:与えないでください。"
for t in table[1:]:
t2 = t.split()
answer = "【"+t2[0]+"】"
answer += '\n生食='+t2[1]+', 加熱='+t2[2]
answer += '\n\n'+explanation
botanswer[t2[0]]=answer
with open(OUT_FILE,'w') as f:
json.dump(botanswer, f, indent=2, ensure_ascii=False)