Effectively Using APIs

Categories: Tech | Pubby Cash Received:  0 | Click to Award

There's a product you want really bad, but it's sold out. And every time it becomes available again, you're always too late. Sounds like you need to do some coding that will track that website and whenever that product becomes available, your code will email you, letting you know. But doing so isn't that straightforward, as popular websites know this and will block you from scraping their data. That's when you turn to their API (or application programming interface). Most popular websites provide you with an API key which you can use in your code to query their data. However, there is a catch. Websites do not like programs (like the one you're going to make) that repeatedly access their website. It gets annoying very fast because that website has to serve other people and programs, not only you. So when such occurs, the website will block you yet again, saying that you have gone over the limit. This is why it is important to continuously monitor and revise your code so there aren't any uncontrolled infinite loops, for example. APIs are hosted on web servers. When you type www.google.com in your browser’s address bar, your computer is asking the www.google.com server for a webpage, which it then returns to your browser. Now when you want to track the price of a product, of course, you're not going to do that! You would write a neat program that does all of the tiring work for you. This data is usually returned in JSON format, but it can come in other formats too. To get the data, you have to request a web server. The server then replies with our data. Let's say you want to track the price of a motherboard you want on Amazon. First, you would need to create an AWS account and activate your Marketplace subscription. Once you get your API keys from there, it's time to use them. We are going to do this using Python. Pop open your Terminal or Command Prompt and type 'pip install amightygirl.paapi5-python-sdk' or 'pip3 install amightygirl.paapi5-python-sdk' to install the API package. Then, use the following Python 3 code and fill in the appropriate blanks with your API keys. In this example, we are searching for products with the name "Harry Potter" on it. Click this link to view the code for this program on GitHub.


Published from: Pennsylvania US
Liked by: H2O 

Daily Deals


MECHANICSBURG WEATHER