Using Python to Fetch API Data into CSV

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys today i'm going to be running you through a small beginner python project where we build a script that uses a public api to capture cryptocurrency data and then write it up to a csv and we're going to be doing this all within about five minutes so let's go all right so first off i've got a empty folder weapon here in visual studio code i'm just going to create a new python folder a python file so i'll just call this crypto.pi and all we're going to want for this is just to import the request module and import the csv module okay now quickly we're just going to jump onto a web browser to find a public api that we can use for cryptocurrencies and if you go to google and just search up public api you can find one over here and you scroll down to cryptocurrency and the one we want to be using today is coincap cool so we'll open up coin cap and then we're just going to get the restful api documentation and we just want to take this endpoint right here now we can say url equals http just like that alright so now all we want to do is just set the headers for our request to accept uh json all right so now we've got everything we needed to actually build our request so we can say response equals request dot request and oops we want to do a get on the url passing in the headers take all the headers and we don't have a payload but we need to actually specify this parameter i'll just do an empty payload so just like that and now i'll save my json equals response dot json and now we'll just print this out okay put up the terminal crypto.pi uh what happened there did i not spot that right oops sorry hi crypto dot pi and that is all our data right there all right sweet so now we have all our data all we need to do is basically put this into an array and then write it out so what we're going to do is just initialize the empty array first so i'll say our data equals empty array all right now if i get the coin cap documentation just to view an example response which is basically what we're getting already anyway we can see here that we're getting our object which has an array called data which contains an array of objects which has all our cryptocurrency data now all i'm really interested in is the symbol the name and we'll get the price as well so just bearing that in mind all right so now we can go for x in my json reference that data array and now basically for each object within the response we're going to build an array and then append that to our our data array so first off we'll capture the the symbol and then the name for this object and then also the price as i mentioned before all right so just x and then price usd and now we can just append this to our our data and we'll just quickly check that out so just use a quick print statement and now we can go to back to our terminal i'll just clear this out actually and then we can type in pi crypto dot pi and then we can see now we've actually got our our data so we've got a bunch of arrays within our our data array all right cool so now we really need to do is just write this out now into a csv so we'll say open and then the file name which we'll call crypto.csv and make sure we open this up in write mode and the encoding will be a utf-8 which is pretty standard and for the new lines we're just doing a uh just just open and close quote and we'll just save this object as f so now we'll just say writer equals csv dot writer f and now we can do now we have now we have to write out our header but we haven't actually got one yet we'll just write this out for now so we'll say write a dot right row csv header and then right at the right rows our data sweet okay so now we'll actually create this the header which is going to be in the csv so we'll say csv header equals an array which has got symbol and then we'll do name and then we'll do uh price usd as well again and that is pretty much literally it so i'll just quickly put a little print done down here and now we should be able to just run it again so we do pi crypto pie sweet so now we can see our csv file has been generated we can actually open this up within via studio code uh but i'll just go ahead and open this up in my file explorer so i can uh just pull it over and you can actually see the csv itself so and we can see all our cryptocurrency data is here and ready for us to read it in a nice format or send it off to whoever all right guys thanks for watching if you found this video helpful make sure you subscribe so you don't miss out on videos just like this
Info
Channel: Daniel Otto
Views: 29,217
Rating: undefined out of 5
Keywords: python tutorial
Id: bHCHKeJ6bI8
Channel Id: undefined
Length: 5min 23sec (323 seconds)
Published: Thu Nov 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.