How to Access Web APIs using Python Requests and JSON
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
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
You're quite welcome - glad I could help. Keep on codin'!
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.
You are quite welcome. I also decided to use python recently instead of fighting Java. Keep on codin'!
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!
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'!
I came here from Pong video and just wanted to say that you are the best at explaining coding. Thanks!
Thanks - much appreciated. Keep on codin'!
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
Glad to help - keep on codin'!
I like the way you explain your code Sir. Makes everything very clear... Thanks... 🎉
Thanks - keep on codin'!
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.
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'!
@@TokyoEdTech Very kind!!!!! Thank you very much!!!!
Simple and easy to follow! Thanks TokyoEdtech
You're welcome - keep on codin'!
thanks, this helped me create a simple program to hopefully get rich in Albion Online
Get rich or die trying - good luck!
man ive been waiting for a video on this
Glad to hear it - enjoy!
This was exactly what I was searching for thank you!
You're welcome. Keep on codin'!
very helpful video! btw, you can s
also do response.json() to convert it to a dict instead lf using json module
Great tip - thanks for sharing that!
You are the best and you don't get what you deserve
Thanks - much appreciated!
sir you teach so well
Thank you, I try.
Good professor ! Thanks a lot !!!
You're welcome - keep on codin'!
that was helpful, thank you a lot :)
You are quite welcome - keep on codin'!
CZcams just notified me about this. I am subbed and it ain't sending me notifications :(
CZcams is mysterious like that!
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
I need to see the code also to help.
Great video! I love it!
Thank you!
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
Glad you caught that...been there, done that!!!
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?
Check out my intro to Python - it'll get you started!
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
This is probably a bit more advanced - watch the previous video about the requests module then try this one.
Hey
I am getting error- list indices must be integers or slices, not str
Can you please help?
Sure. Paste your code here and I'll take a look.
@@TokyoEdTech nvm figured it out thanks for the help
@@乲 Good to hear - keep on codin'!
I know a lot of people have been asking but when are you going to continue space arena
I was hoping to get to it this summer, but it is unlikely due to unforeseen circumstances. Sorry.
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?)
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.
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
Heya - sure thing! Every thing I do is Creative Commons Licensed - please include a shoutout in the game credits. Good luck!
U said yas or no? Idk
But what is "Creative Commons Licensed?"
@@TheSpekeyMan Yes, go ahead. Please Google Creative Commons.
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 :)
MUST you? No. SHOULD you? Yes. Otherwise, why did you bother asking?
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
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.
Thanks for the video.
I have an URL in json. How do I make that URL open in browser?
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.
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?
You'd have to check the API documentation. Then print the result of your query and see the data format and go from there.
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()
That's a good question - did you try your code? What happened?
@@TokyoEdTech not yet I could try it.
@@TokyoEdTech I just tried it, it worked!
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.
Hiya - I'm sure it's possible, but I'm not sure how to go about that one...gotta study more!
Are you having a creative block? Not mad or anything but I'm just wondering when your next video will be?
Nope. Just a lot of personal stuff going on. Hoping to get me stuff out next month...
Do you know how to scrape Userinfo of Quora user(Without Using Requests-HTML or Selenium)?
Sorry, no. I've used Selenium in the past, but it was a looooong time ago.
@@TokyoEdTech i hate selinum too that's I don't want to use it😕
Hang in there!
Which editor you are using?
You’re in luck - I’ve made a video about this exact topic. Enjoy! czcams.com/video/4bL-lCP2tec/video.html
@@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?
Could you try making Chess? 🤔
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
In the olden days of minecraft pe..... mods were made in json! They are now made in c++ (I think)
Yep they are
Ahh...the olden days!
can you make formula 1 game in python pleas
What did you have in mind? Do you have a link?
@@TokyoEdTech Yes i have czcams.com/video/v2nq-IGz_JQ/video.html
You should start a reddit! That way its easier to help people!
Thanks, but I barely have time to keep up with my channel these days!
@@TokyoEdTech I'm so sorry I didnt think about that!
@@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!
sir plz plz plz make animation python 3 part 7
Not sure what else to the animation video.
Hey
I am getting error- list indices must be integers or slices, not str
Can you please help?
Sure. Paste the code here and the exact error message.