まずは読んでみよう
qiita.com
うむ、わからん!!
みんな、VisualStudioCodeとPython3はインストールしたね?!
Visual Studio Code でPython入門 【Windows編】: Visual Studio Code でPython入門 【Windows編】 - python.jp
この辺参考にやってみよう!(自分がどう環境構築したかは忘れた)
さて、最初の記事で唯一わかりそうなのはエンドポイントってURLやな。
https://versatileapi.herokuapp.com/api
このままアクセスしても、500 Internal Server Errorですな!! なんのことやらさっぱりです。
うむ! わからん!!
でもたぶんGETって最初のページに書いてあったよねーというわけで
- ODataクエリによる検索(最新20件取得)
ってのをやってみる
- 駄目な例
import requests url = "https://versatileapi.herokuapp.com/api" r = requests.get(url, params="/text/all?$orderby=_created_at desc&$limit=20") print(r.json())
??? パラメーターの使い方がわからんね。でもコレってURLっぽいから・・・
import requests url = "https://versatileapi.herokuapp.com/api" r = requests.get(url + "/text/all?$orderby=_created_at desc&$limit=20") print(r.json())
取れたけどコレってhttps://versatileapi.herokuapp.com/api/text/all?$orderby=_created_at desc&$limit=20ってことよね? パラメータとは一体・・・。
(゜-゜)・・・
import requests url = "https://versatileapi.herokuapp.com/api/text/all" r = requests.get(url ,params={"$orderby":"_created_at desc","$limit":"20"}) print(r.json())
こういうことか!!! あたり!!!
jsonとか言うのをいい感じに表示するのは・・・
google:json python 表示
今回は
Python Tips: JSON を整形して表示したい - Life with Python
からコピッペして
でたな文字コード!!
参考:Pythonでjson dumpsを使いこなそう!(encoding、foramt、datetime) | 侍エンジニアブログ
import requests import json url = "https://versatileapi.herokuapp.com/api/text/all" r = requests.get(url ,params={"$orderby":"_created_at desc","$limit":"3"}) print(json.dumps(r.json(), indent=2, ensure_ascii=False))
こんな感じで取れる。絵文字はともかく\nはこのままで良いのかな? よくわからんなぁ
{
"id": "31a70ac2-4cfc-425b-a47c-5728d0fde670",
"_created_at": "2021-07-21T19:38:46.490+00:00",
"_updated_at": "2021-07-21T19:38:46.490+00:00",
"_user_id": "2f38f9b8f278f43fc53d143f31ff805e9dbbd469",
"text": "🌞🥵\n🌞🤯 🌞💨\n🌞🤢🌞\n🌝💩🌝"
},
表示をどうにかしたいのでまた今度