This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

Linebot用に犬の食べ物リストをつくる

Posted at

概要

自作Linebot用のデータベースとして使うことを念頭に、犬に食べさせてはいけないもののリストを作成しました。
情報は犬に食べさせてはいけない物(138品目) | 迷子ペット.NETを参考にいたしました。

環境

macOS Catalina 10.15.4
python 3.8.0

表の情報をテキストファイルにコピー(手動)

ホームページの表の情報を手動でテキストエディタにコピペします。多分スクレイピングするよりも手動のほうが早いです。
スクリーンショット 2020-09-22 0.08.24.png

情報の成形

テキストファイルの文字列を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)
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up