How to Access Web APIs using Python Requests and JSON

Sdílet
Vložit
  • čas přidán 9. 09. 2024
  • In this video I'll show you how to quickly and easily access web apis and convert the resulting JSON data into a Python dictionary, pull the relevant information out, and print it. We'll be using TV Maze to get information about various TV shows.
    Links:
    TV Maze API Info: www.tvmaze.com...
    JSON: www.json.org/j...
    Code:
    import requests
    import json
    import pprint
    url = "api.tvmaze.com/..."
    show = input("Please input a show name. ")
    params = {"q":show}
    response = requests.get(url, params)
    if response.status_code == 200:
    data = json.loads(response.text)
    pprint.pprint(data)
    name = data['name']
    premiered = data['premiered']
    summary = data['summary']
    print(f"{name} premiered on {premiered}.")
    print(summary)
    else:
    print(f"Error: {response.status_code}")
    NEED HELP?
    🆘 Watch this first and then let me know in the comments below: • Help Me Help You
    ❤️❤️ SHOW SOME LOVE AND SUPPORT THE CHANNEL ❤️❤️
    Click Join and Become a Channel Member Today!
    Channel members can get preferential comment replies, early access to new content, members only live streams, and access to my private Discord.
    ❤️ / @tokyoedtech
    LINKS
    🗄️ GITHUB: github.com/wyn...
    💬 Follow me on Twitter: / tokyoedtech
    📜 Subscribe to my Newsletter: eepurl.com/dKgM8k
    📝 Check out my Blog: christianthomp...
    ⬇️ Download Geany Editor: www.geany.org
    LEARN MORE PYTHON
    ➡️Space Invaders: • Python Game Programmin...
    ➡️Snake Game: • Python Game Programmin...
    ➡️Pong: • Python Game Programmin...
    ➡️Space War: • Python Game Programmin...
    ➡️Intro to Python (for Java Coders): • Intro to Python for Ja...
    ➡️Space Arena - The Ultimate Python Turtle Graphics Game Tutorial: • Ultimate Python Turtle...
    LEARN MORE JAVA
    ➡️Basic Java for Beginners: • Basic Java 1&2: Commen...
    ➡️Intro to AP Computer Science A: • AP Computer Science Un...
    #Python #Tutorial #API #JSON

Komentáře • 97

  • @abinsyaseen7298
    @abinsyaseen7298 Před 3 lety +2

    I learned to make a simple game from the turtle module from you i think a year ago and thats how i started my python journey...and then learnt a lot of stuff like data science and i was thinking to learn about accesing web api s and this video got recommended to me and i had a quick glance on my journey...thanks a lot mate

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety +1

      You're quite welcome - glad I could help. Keep on codin'!

  • @lonelypizzaslice4845
    @lonelypizzaslice4845 Před 2 lety +1

    Tysm for this tutorial! I appreciate how clear and straight-forward it was. I have a project coming up and after some googling, decided it would be quicker to completely relearn HTTP requests in python rather than fight the other language I was using lol.

    • @TokyoEdTech
      @TokyoEdTech  Před 2 lety

      You are quite welcome. I also decided to use python recently instead of fighting Java. Keep on codin'!

  • @PLAYCOREE
    @PLAYCOREE Před 6 měsíci +1

    Thanks for helping me out, just started this week learning Python as my first language. I started the first time coding with js but i'm really not a very motivated learner when i dont have a goal to reach.
    For my first learning project i wanted to make a fake donation programm for Twitch streamers using the stream elements api and a simple GUI with customTKinter. Really didnt know why i was having problems with it since i thought it was returning a dictionary. Now i know, appreciate your content even tho this video is 2 years old!

    • @TokyoEdTech
      @TokyoEdTech  Před 6 měsíci +1

      It's always good to have a goal, but sometimes you gotta buckle down and do the hard work to make progress. Thanks for the comment and keep on codin'!

  • @OK-69420
    @OK-69420 Před 3 lety +1

    I came here from Pong video and just wanted to say that you are the best at explaining coding. Thanks!

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      Thanks - much appreciated. Keep on codin'!

  • @preciousudo415
    @preciousudo415 Před rokem

    You have literally saved me from failing my class. The explanation is better than what my professor was trying show in the class. Thank you

  • @user-hd7zg3rj3b
    @user-hd7zg3rj3b Před 7 měsíci +2

    I like the way you explain your code Sir. Makes everything very clear... Thanks... 🎉

  • @luiscarlostrotta8119
    @luiscarlostrotta8119 Před rokem +1

    good afternoon!!! nice to meet you, I love your didactics. I'm from Argentina and I'm not fluent in English, but thanks to Python and tutorials of such quality as this one, I'm doing well.
    very good explanation, I'm interested in everything about python!!!!, thank you very much, greetings.

    • @TokyoEdTech
      @TokyoEdTech  Před rokem

      Gracias! Glad to hear it is helpful. Here is a link to my Intro to Python book (pdf). drive.google.com/file/d/1ajYJZLGUaVmNbuG98LnRfHMTzvnZx9el/view?usp=drivesdk
      Keep on codin'!

    • @luiscarlostrotta8119
      @luiscarlostrotta8119 Před rokem +1

      @@TokyoEdTech Very kind!!!!! Thank you very much!!!!

  • @bvladimirpl
    @bvladimirpl Před 2 lety +1

    Simple and easy to follow! Thanks TokyoEdtech

  • @CloroxBleach-vc1pw
    @CloroxBleach-vc1pw Před rokem +1

    thanks, this helped me create a simple program to hopefully get rich in Albion Online

  • @jonathanantfolk7023
    @jonathanantfolk7023 Před 3 lety +1

    man ive been waiting for a video on this

  • @thebdceigal
    @thebdceigal Před rokem

    This was exactly what I was searching for thank you!

  • @seadve
    @seadve Před 3 lety +1

    very helpful video! btw, you can s
    also do response.json() to convert it to a dict instead lf using json module

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety +1

      Great tip - thanks for sharing that!

  • @berkayavclar110
    @berkayavclar110 Před 3 lety

    You are the best and you don't get what you deserve

  • @trending1360
    @trending1360 Před 3 lety

    sir you teach so well

  • @PlaneteAdos
    @PlaneteAdos Před rokem

    Good professor ! Thanks a lot !!!

  •  Před 2 lety

    that was helpful, thank you a lot :)

    • @TokyoEdTech
      @TokyoEdTech  Před 2 lety +1

      You are quite welcome - keep on codin'!

  • @thech0sen1
    @thech0sen1 Před 3 lety +1

    CZcams just notified me about this. I am subbed and it ain't sending me notifications :(

  • @stephendoamekpor1671
    @stephendoamekpor1671 Před 2 lety +1

    This is what I get when I run the code.....need help.
    Traceback (most recent call last):
    File "c:\Users\edemd\Documents\DoamPrograms\py4e\practice\workpage.py", line 15, in
    name = data['name']
    TypeError: list indices must be integers or slices, not str

    • @TokyoEdTech
      @TokyoEdTech  Před 2 lety +1

      I need to see the code also to help.

  • @diyeyejohson3070
    @diyeyejohson3070 Před rokem

    Great video! I love it!

  • @Butssy
    @Butssy Před 2 lety +1

    hi, i have a question, when i do the pprint.pprint, it comes up as an error, and says it is undefinied. is there any reason why this would happen?
    edit: simple mistake, forgot to import pprint

    • @TokyoEdTech
      @TokyoEdTech  Před 2 lety +1

      Glad you caught that...been there, done that!!!

  • @fx572
    @fx572 Před 3 lety

    I was wondering if I could have your opinion on something: I’ve been using an animation program for a few months now called Source Filmmaker where you do 3D animation on. I wanted to script for some IK rigs so I looked around on Google to find out how to that when I found this article that shows you how to do that. I read it and right off the bat, the guide told me I needed Python for making the scripts and saving them into Source Filmmakers directory. I spent mostly all of today trying to figure out how to use it and I’ve pretty much have come to the conclusion that I’m gonna have to learn Python step by step. What would you suggest I try?

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      Check out my intro to Python - it'll get you started!

  • @devanshujoshi8393
    @devanshujoshi8393 Před 3 lety +1

    What is this I didn't understand I subscribed your channel after learning to build games and learnt python , is this something advanced for a python intermediate or beginner

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      This is probably a bit more advanced - watch the previous video about the requests module then try this one.

  • @乲
    @乲 Před rokem +1

    Hey
    I am getting error- list indices must be integers or slices, not str
    Can you please help?

    • @TokyoEdTech
      @TokyoEdTech  Před rokem

      Sure. Paste your code here and I'll take a look.

    • @乲
      @乲 Před rokem +1

      @@TokyoEdTech nvm figured it out thanks for the help

    • @TokyoEdTech
      @TokyoEdTech  Před rokem

      @@乲 Good to hear - keep on codin'!

  • @gummies579
    @gummies579 Před 3 lety +1

    I know a lot of people have been asking but when are you going to continue space arena

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      I was hoping to get to it this summer, but it is unlikely due to unforeseen circumstances. Sorry.

  • @sand3514
    @sand3514 Před 7 měsíci

    Hello! I have recently started to learn things about APIs (what they are, different kinds of requests, etc) and as ive seen, Postman is used alot by data scientists to send HTTP requests to APIs. What are the pros of using Postman? (As ive seen, it sends a http request just as you showed in the video, the only thing that i can notice is that Postman is more efficient, but what other pros does it have?)

    • @TokyoEdTech
      @TokyoEdTech  Před 7 měsíci +1

      Hi! From what I can tell, Postman is a full-featured API system that not only provides APIs, but also allows for testing and troubleshooting. What I showed in this video is the barest example to explain the concept.

  • @TheSpekeyMan
    @TheSpekeyMan Před 3 lety +2

    Hey man I wanted to ask something
    "I used ur code to make a game (Snake Game) and also modified it a little bit. ("Can i copyright and publish it to Microsoft Store or The Play Store? ") PLS answer me

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      Heya - sure thing! Every thing I do is Creative Commons Licensed - please include a shoutout in the game credits. Good luck!

    • @TheSpekeyMan
      @TheSpekeyMan Před 3 lety +1

      U said yas or no? Idk
      But what is "Creative Commons Licensed?"

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      @@TheSpekeyMan Yes, go ahead. Please Google Creative Commons.

    • @TheSpekeyMan
      @TheSpekeyMan Před 3 lety

      And I also wanted to ask "MUST i shout u out in the credits?"
      That was my last question. THX for the snake tutorial video man.
      And NO-OFFENCE asking u the last question. BYE :)

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety +1

      MUST you? No. SHOULD you? Yes. Otherwise, why did you bother asking?

  • @valluriseshapraneeth3469
    @valluriseshapraneeth3469 Před 7 měsíci

    Hi sir,
    I have a url with API key how can i retrieve the data, You have placed the last part of the URL as parameters.
    Where can I put the API key

    • @TokyoEdTech
      @TokyoEdTech  Před 7 měsíci

      Hiya. The API key is usually a parameter as well. You'll have to check the site documentation to figure out how to use it.

  • @azghar.suhail
    @azghar.suhail Před 2 lety

    Thanks for the video.
    I have an URL in json. How do I make that URL open in browser?

    • @TokyoEdTech
      @TokyoEdTech  Před 2 lety

      First pull the URL out of the json data
      Second, follow the directions here: stackoverflow.com/questions/4302027/how-to-open-a-url-in-python#:~:text=just%20open%20the%20python%20interpreter,it%20does%20what%20you%20want.

  • @laredoeneko
    @laredoeneko Před 8 měsíci

    How do you search in the API? imagine search for all the movies/series from the year=2010 for example, in this case the reponse is not unique, how doest python manage it?

    • @TokyoEdTech
      @TokyoEdTech  Před 8 měsíci

      You'd have to check the API documentation. Then print the result of your query and see the data format and go from there.

  • @unbreakable7643
    @unbreakable7643 Před 3 lety

    I have a question for you about functions. Can you put a function inside a function? For Example:
    def hello():
    print("hello world")
    print("hello #2")
    def IdontKnowanyMore():
    print("I dont know anymore")
    hello()

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      That's a good question - did you try your code? What happened?

    • @unbreakable7643
      @unbreakable7643 Před 3 lety

      @@TokyoEdTech not yet I could try it.

    • @unbreakable7643
      @unbreakable7643 Před 3 lety

      @@TokyoEdTech I just tried it, it worked!

  • @kavitham4526
    @kavitham4526 Před 3 lety

    Hi, I learned about python and also some gaming programs which is very helpful for me and also for everyone. I need a help from you as a guide.. Is there any possibility to write a code to convert text to animation video...If it is so, then it will be really helpful for me.

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      Hiya - I'm sure it's possible, but I'm not sure how to go about that one...gotta study more!

  • @colwarsstudiobrickfilmandm8580

    Are you having a creative block? Not mad or anything but I'm just wondering when your next video will be?

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      Nope. Just a lot of personal stuff going on. Hoping to get me stuff out next month...

  • @siddhartha.01
    @siddhartha.01 Před 2 lety

    Do you know how to scrape Userinfo of Quora user(Without Using Requests-HTML or Selenium)?

    • @TokyoEdTech
      @TokyoEdTech  Před 2 lety

      Sorry, no. I've used Selenium in the past, but it was a looooong time ago.

    • @siddhartha.01
      @siddhartha.01 Před 2 lety +1

      @@TokyoEdTech i hate selinum too that's I don't want to use it😕

    • @TokyoEdTech
      @TokyoEdTech  Před 2 lety +1

      Hang in there!

  • @ayanupadhaya6972
    @ayanupadhaya6972 Před 3 lety

    Which editor you are using?

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety +1

      You’re in luck - I’ve made a video about this exact topic. Enjoy! czcams.com/video/4bL-lCP2tec/video.html

    • @ayanupadhaya6972
      @ayanupadhaya6972 Před 3 lety

      @@TokyoEdTech I want a help from you. As I program in python I use pip to install packages. Currently using linux mint mate os. I don't know what is the problem whenever I try ton install a package using pip3 it takes 2 or 3 minutes where it would take few seconds in windows 10 to download a package. Any solution?

  • @montagetm
    @montagetm Před 3 lety +1

    Could you try making Chess? 🤔

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      It's definitely on my to-do list. I started a text-based version a while ago, but never got back to it. LINK: pastebin.com/4VNcyHru

  • @colwarsstudiobrickfilmandm8580

    In the olden days of minecraft pe..... mods were made in json! They are now made in c++ (I think)

  • @berkayavclar110
    @berkayavclar110 Před 3 lety +1

    can you make formula 1 game in python pleas

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      What did you have in mind? Do you have a link?

    • @berkayavclar110
      @berkayavclar110 Před 3 lety +1

      @@TokyoEdTech Yes i have czcams.com/video/v2nq-IGz_JQ/video.html

  • @colwarsstudiobrickfilmandm8580

    You should start a reddit! That way its easier to help people!

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      Thanks, but I barely have time to keep up with my channel these days!

    • @colwarsstudiobrickfilmandm8580
      @colwarsstudiobrickfilmandm8580 Před 3 lety +1

      @@TokyoEdTech I'm so sorry I didnt think about that!

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      @@colwarsstudiobrickfilmandm8580 No worries - I'm a relatively small CZcamsr and get lots of requests for help. I can't imagine what larger channels have to deal with!

  • @trending1360
    @trending1360 Před 3 lety

    sir plz plz plz make animation python 3 part 7

    • @TokyoEdTech
      @TokyoEdTech  Před 3 lety

      Not sure what else to the animation video.

  • @heckergrp
    @heckergrp Před rokem

    Hey
    I am getting error- list indices must be integers or slices, not str
    Can you please help?

    • @TokyoEdTech
      @TokyoEdTech  Před rokem

      Sure. Paste the code here and the exact error message.