1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ChatGPT先生おすすめのAPI

Last updated at Posted at 2024-12-21

はじめに

この記事は、気になるWeb API使ってみた Advent Calendar 2024の25日目の記事です。

ChatGPTは世の中の多数のAPIの情報を学習しているはずなので、おすすめのAPIを聞いてみようという企画です。

ChatGPT先生に聞いてみる

スクリーンショット 2024-12-20 21.19.59.png
スクリーンショット 2024-12-20 21.20.07.png
スクリーンショット 2024-12-20 21.20.16.png
スクリーンショット 2024-12-20 21.20.24.png
スクリーンショット 2024-12-20 21.20.34.png
スクリーンショット 2024-12-20 21.20.44.png
スクリーンショット 2024-12-20 21.20.54.png
スクリーンショット 2024-12-20 21.21.03.png

1、4、5、7は、言葉が出るタイプかな。
ChatGPT先生は言葉が出てくるAPIがお好み?

5の Bored APIはエラーになった。

1、4、7のAPIからデータを取得してみよう。

スクリーンショット 2024-12-20 21.21.19.png
スクリーンショット 2024-12-20 21.21.33.png

catfact.py
import requests

def get_cat_fact():
    url = "https://catfact.ninja/fact"  # Cat Facts APIのURL
    try:
        response = requests.get(url)
        response.raise_for_status()  # エラーがあれば例外を発生
        data = response.json()  # JSONデータを取得
        ![スクリーンショット 2024-12-21 14.21.34.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1032713/e477045b-8941-62ad-9aec-3b04430e7e36.png)
print("🐱 猫のトリビア:")
        print(data['fact'])  # トリビアを表示
    except requests.exceptions.RequestException as e:
        print(f"エラーが発生しました: {e}")

# 実行
if __name__ == "__main__":
    get_cat_fact()

実行してみる。

スクリーンショット 2024-12-20 21.06.42.png

にゃるほど。

次のAPIも試してみよう。
スクリーンショット 2024-12-21 14.21.27.png

スクリーンショット 2024-12-21 14.21.34.png

AdviceSlip.py
import requests

def get_random_advice():
    url = "https://api.adviceslip.com/advice"  # Advice Slip APIのURL
    try:
        response = requests.get(url)
        response.raise_for_status()  # HTTPエラーを確認
        data = response.json()  # JSON形式のレスポンスを解析
        print("💡 今日のアドバイス:")
        print(data['slip']['advice'])  # アドバイスを取得して表示
    except requests.exceptions.RequestException as e:
        print(f"エラーが発生しました: {e}")

# 実行
if __name__ == "__main__":
    get_random_advice()

猫の時は猫のアイコン、アドバイスの時は電球のアイコンを勝手に入れてきている>ChatGPT
すごいな・・。

これも実行してみる。

スクリーンショット 2024-12-20 21.07.11.png

If you want to be happily married, marry a happy person.

確かにそうかもしれない。相手から見たら、自分がhappy personである必要があるということと感じた。

次行ってみよう。

スクリーンショット 2024-12-21 14.46.26.png

ChuckNorrisJokes.py
import requests

def get_chuck_norris_joke():
    url = "https://api.chucknorris.io/jokes/random"  # Chuck Norris Jokes APIのURL
    try:
        response = requests.get(url)
        response.raise_for_status()  # エラーがあれば例外を発生
        data = response.json()  # JSONデータを取得
        print("🤣 Chuck Norris Joke:")
        print(data['value'])  # ジョークを表示
    except requests.exceptions.RequestException as e:
        print(f"エラーが発生しました: {e}")

# 実行
if __name__ == "__main__":
    get_chuck_norris_joke()

スクリーンショット 2024-12-21 14.50.19.png

うむ、これがアメリカンジョークであるか?

まとめ

ChatGPTのお気に入りのAPIは、何か言葉をランダムに出してくれるAPIの様子。
単純なAPIアクセスはChatGPTでコード生成できる。

以上、ChatGPT先生おすすめのAPIでした。

最後までご覧いただきましてありがとうございました。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?