Coding With Python :: Learn API Basics to Grab Data with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright so in this one we are going to use Python and a website's API so an API is something that any particular website could just design this thing called an API to give out their data and allow your web application to communicate with that data and possibly even make new data too so like Facebook has an API Twitter does I mean Yelp so many services that rely on data have api's and they're usually pretty easy to work with and pretty straightforward in this case we're going to be using the locu api which is as it says real time local business data which is kind of kind of cool so go ahead and sign up for it this is more or less specifically an API so devil oku com and then you're going to sign up and then once you sign up it's going to take you into the developer dashboard and it's probably gonna have you fill out a lot of stuff but anyways the main thing you want to do is you want to grab this API key so go ahead and copy that and we're going to open up a new Python file here I'm using Komodo edit I think it's a pretty easy text editor for people so with that being said now that I have my API key you could use mine I might have to reset it at some point but it's just better for practicing purposes to have your own API key because if you're using somebody else's you might run into issues like it doesn't work anymore or like I said if I reset this or have them change it for me then it's going to no longer work on your end and also slot it right out and stuff so it's probably easier just to get your own all right so now back in coda it now that we have a just a regular old Python file open an easy way to do that as file new and then python py or if you go to from template I'm using Python 2.7 so keep that in mind when you're going through here now let's actually do a little discovery after we paste this in here let's paste our API key so loku api and it's just that string so I'm just going to save that for now I have it saved as API py so we'll just keep that in mind for now but then let's go back into here and I'm going to look at their API console now this console is just basically showing you what data you can grab from their service it's very easy and simple and I mean it's it's pretty straightforward as to what you're doing you're doing this quick little search so in this case it's a venue search and you can search by all these different things or you could say by our region so let's say Newport Beach where I am I can search Newport Beach type that out it does little search for me it says how many limited and then it has like objects and it has all this different stuff you know hair by Cortney obviously does not have a menu and then Oasis you know all these different things are in there and then of course we could kind of do some other stuff if we wanted to on here like what the website URL is and if it has a menu or not if I do true it's going to do a new search and that will most likely bring back food then of course you could change this to whatever category might fit into and the way you would know what categories are available of course you just try out something or you could do a search and see Oh restaurant is a category even and I can get rid of has menu and now it's doing all these like restaurant searches in Newport Beach now of course depend on what part of the world you're in it might not have everything like a Dell Rome has anything on there Rome Italy that is this looks like Rome Georgia so let's go back to country in search Rome Italy it looks like it has a few things in here too yeah and maybe those are accurate or not I'll leave that up to you guys to kind of check that out but in this case I'm going to be using just Newport Beach as my locality for grabbing this data now something important to note right off the bat is this right here this is the query so this is what it's actually looking up it's sending this to their server and then that server is given a response and then this is the response so if we copy this URL and paste it up here in a new tab we we see the same exact information so that's what we see here right so that's all this stuff it's not as pretty but it it's it's still there it's definitely there so now that we see this information right we can we can use it since this URL is real and it's actually working we can open this data inside of Python so opening up Python I have it open in I have it open up in terminal so if I exit out and then type Python it brings me into Python 2.7 point 5 if you don't how to install Python look for one of my other videos because I show you actually how to install Python it's pretty easy for Mac users Windows users it's easy to there's just a few extra steps and ok so now that we have this I'm going to import this library called URL Lib - so URL Lib and URL Lib - are not the same things you're Alip - I believe is the most current version so it's the most supported and it has the least amount of bugs so just kind of keep that in mind and you are a Lib - it has a function in it that allows me to open up a URL so when we go here it's actually opening up a URL so if we copy this domain and we do URL Lib - URL open and then we put that URL in there it's going to open that URL right and the problem that we see now is that we don't know what all this stuff is now I already know what this is because it's in a dictionary and what that dictionary how I know it's in a dictionary because of how it looks so when we look at this we see that there's this little curly bracket and we see those throughout this whole thing there's curly brackets on the outside and if you look again on the website there's curly brackets everywhere so that means it's a dictionary which also means that it's JSON so JSON like that JSON it's pronounced JSON almost like Jason without an a J Shawn okay so that is JSON data and in order for us to make it useful we need to kind of change the way it looks we need to load that JSON data so we're going to import JSON and then we're going to do JSON let's actually print JSON load and then now I want to load this entire thing so I'm going to copy that and paste it in here make sure you have the trailing parentheses and we're going to press Enter and look at that now we can see all of that same data in our Python interpreter so we can now use a lot of this data but how are we going to use it so let's actually make this a little bit better by writing the actual code down so we have this API key and then if I paste out that URL open that I just copied I can paste that in here I'm actually cut this and paste that above and say URL equals two and then I'm going to have this long string for now we'll make that a little bit more dynamic later but let's just keep that as is now okay so then I'm going to just say some arbitrary object is going to be opening this URL Lib we could also call it a JSON object right because that's what's we expect to be returned from this particular API most api's or a lot of api's will actually come out that way it'll look just very similar to this so just kind of keep that in mind when you're playing around with it so now that we have the JSON data we again have to import everything here so import URL live too and also import JSON like we did in the Python interpreter and then what we want to do is load this JSON object so the data is going to be JSON load and JSON object okay so this thing JSON object is if we let's change that to data so if we print out data right so copying all this stuff and printing it back in here it prints the same thing so how do we work with this now dictionaries are kind of cool because they are able to be called in this case we have like the two like kind of parent keys here so the top parts are we have meta and objects and if we scroll down we don't actually have anything else most of the stuff is actually in inside of objects all right so let's let's show you what I mean by that so if I just print data and then objects right if that's what I'm printing so let's go back in the terminal then if I scroll to the top I'm not going to see Maeda Mehta anymore right whereas if I go up one more of them scroll to the top of that you see meta so I just called that one objects so if I go back in here I see there's objects here okay and right off the bat I see that there's this you know these other brackets to squared off brackets and python that means that it's a list right so if things are like this list one list two so on and so forth that's a list right so we can iterate through that this could also be a json dictionary right for each list item and that's what we see so if we scroll all the way back down we see that there's there's list items in here so there's actually stuff within that list so let's actually it right through the item so for item in data objects let's just print item and what this should do for us is show us each item and now being kind of outside of the dictionary list right so each item is now its own separate line item okay so I can also just print item and say like item is and then we can add in the item go through here and opie you can concatenate those two things so let's turn it into a string and there so now it's saying item is and then the string we turn the dictionary into a string so you can see it and each one says item is okay cool so now each item basically in this case has its own dictionary right so if we look back in here we see objects and then there's list of objects and if we look at the very first one it shows us that bracket starts there and then it closes there so that's that full first item okay so that is clear that it's a dictionary and since it's print it's all kind of clean for us we can we can see that it's that first list item because this bracket is not closed and we see a comma here and then we could go down and here's another one now logically - you could look at this and say okay objects in the name and then it says this and if I scroll down a little bit I see another one says name and it says something different so they're probably not the same thing and therefore they're probably in a list okay cool so what we can do then is item name so now it's going to look through and use that dictionary so it's going to look for that key and then it's going to give us the value of that name so this is the key this is the value and this is a key this is the value so you could do that over and over with each one of these keys right it's it's pretty straightforward as to how that stuff's going to work so if we copy this go back into terminal and paste it out there goes it shows us Johnny's real New York Pizza Newport Beach that's obvious so we used the locality of Newport so let's actually use phone instead so we go back into come out of edit change this to phone copy all this paste it there we go so now we have the name and the phone of that particular place now if you if you saw like how to do a c CBS file or something like that this part would be really easy now you can start pulling all this data and use it for your site of course if you have Jango or something like that - you could use this data in your views and it's just a simple little thing right here so what I'm going to actually do is is clean this up a little bit I'm going to change how the URL is and then I'm going to allow you guys to do your own search for a locality so I'm going to turn it into a function so I'm going to say locu search right and it's going to take in a query and then we want our API so our API key basically is going to be equal to the low qu API okay so once you have that then you have your URL it's definitely going to be this part right so that's going to be one part of it but then we also need to make sure that we have our API key in here and if we take a look we're going to want to make sure that we have this question mark so let's have that at the end and then we noticed locate locality equals that and then it says an category equals that and API key equals that so if we do the first one being API underscore key equals two and then we add the API key that we'll cover that will basically eliminate all these and just be like that right so that should kind of make sense and then what we have left over is this little thing so let's figure out the locality so locality equals two it's going to be something about the query right so we're going to have to kind of clean that up but for now let's leave it as that so whatever you're searching in here that's what the locality is going to be and then we'll say we can say final URL equals to URL plus we're going to have to do and locality right so this and locality equals two and then we add that locality there too so now it's a little bit smarter you could put this like the locality search here but one thing that I want to make sure that I do is I want to replace a space so any spaces with % 20 so that represents a space and I noticed how to do that because of this right I searched Newport Beach but there was a space already in that URL because spaces in URLs just don't really play nice so you want to make sure it's percent 20 if there's a space in some cases you would you could probably even use a - that might even work in this but we're going to follow what they did and that was they changed it to percent 20 cool so now that we have that I'm also going to add an category of restaurant so I'm going to do plus and then put that into quotes - now we have that final URL I can take some of this stuff out and put it into our function here and we want this still so we're going to copy that and change this URL to final URL there we go and then we'll do print item name and then that's that's pretty much all we're going to do is it's going to print the name of the restaurant and then the phone of course you could have try statements because if there is no phone number it it's going to might run an error but for now this is this is good so let's go ahead and copy this I'm actually going to bring this down and or well actually I'll delete all this stuff just so it's nice and clean for us so I'm going to copy all of this exit Python and then go back into Python just to make sure that everything's kind of cleared out I paste everything it looks like we have an indention error and that's because I entered it out so now that I fixed that that function should work there we go so now that function I can do locu search so locu search and the query I could say New York press ENTER locu hope I spelled it wrong and local search global name final URL URL is not defined and again there's another error so obviously kind of kind of keep your eye out as to what's going on these types of errors happen all the time so just make sure that you know that and I could find it pretty quick because I see global names not defined and I know I defined final URL so you know finding that was actually pretty simple so locu search and then new york press enter and there you go now you have all these restaurants and you can do that and you can repeat this same exact process for virtually any API you just want to make sure that you have your API keys and you're designing your URL as they tell you to design it so yeah that's a basic API search if you have any questions let me know otherwise we will see you in the next one
Info
Channel: CodingEntrepreneurs
Views: 664,047
Rating: undefined out of 5
Keywords: try django, try django tutorial, django tutorial, install django on mac, install django with pip, install django with virtualenv, virtualenv, python django, Python (Software), web application development, learn django, installing django on mac, pip, python package, django, kickstarter funded, beginners tutorial, installingdjangocfe2014, django 1.6, codingwithpython2014, code with python, coding with python
Id: pxofwuWTs7c
Channel Id: undefined
Length: 19min 23sec (1163 seconds)
Published: Wed Feb 12 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.