2026-08-26 16:29
Status:
Tags: IBM IBM Full Stack Software Developer Professional Certificate IBM Python for Data Science, AI & Development Python Data Science APIs Data Collection Web Scraping Python
PDSAD - Module 5 - APIs and Data Collection
==Simple APIs
-
Application Programming Interface (API):
-
Application Programming Interface (API) lets two pieces of software talk to each other
-
Basically you can have your program, some data you want to extract or send, and then another software component
-
You can use the API to communicate this data using inputs and outputs, so imaging for a front end and a back end, the two are both endpoints inside of your API, and they send data to each other via the API which basically transfers said data over the internet, or even directly as a translation on your machine
-
You do not need to know how the API works (but what if I want to IBM wtf?), you just need to know the inputs and the outputs
-
Pandas is a set of software components, much of which are not really even written in python, but you have some data in your python program, and then you use the Pandas API to send it over to the processing software and it gives you back your results
-
This is why third party libraries might be faster than just straight up python code, because the libraries aren’t even python themselves
-
-
Basically in the above hand image, you import the Pandas api and then you, in python, create yourself a dictionary
-
Then, whenever you call the
.DataFrame(dict_)function, this dictionary is known as an instance and is sent over to the Pandas API so that way you can get back a dataframe object which is used to communicate further with the API -
This dataframe is basically just a storage object that exists inside of python but is used to communicate again with the Pandas API, so whenever you want to do something like
df.mean()you send the data over to Pandas API, it calculates that bitch, and then sends it back -
REST APIs are another popular type of API which allow you to communicate through the internet, letting you take advantage of things like cloud storage and all the good stuff that comes with being able to send things over the internet
-
RE stands for representational, S stands for state, and T stands for Transfer
-
Basically, these exist as a way of transferring the state of different objects, usually using something like JSON maybe but in this case might be other things because of Data Representation across the internet
-
There would also be things like RESTless APIs which also transfer things over the internet but without the actual state portion of the application
-
Any-who, side tangent aside, the video doesnt mention RESTless APIs I just wanted to talk about them because I think they fit
-
REST APIs are used to interact with web services, in which you are the client and the server is well, the server, or a resource
-
There are a few rules regarding communication, input or response, and output or response see the previous IBM Developing Back-End Applications with Node for more on APIs and what not, at least I think the notes are there
-
-
In the above hand image, you, the user, are the client, and the resource is the place holding whatever data it is that you want to access, you would basically make a
HTTP Requestover to the client at a given endpoint that would allow you to connect and securely transfer over whatever data you were requesting as long as you were authorized to do so -
HTTP Methods, you basically send over a request via this Hyper Text Transfer Protocol, or its more secure cousin. This request is basically just usually a JSON file, and requests the data that you want
-
The backend or whatever you are trying to request will then process if the things you want to get access to are in fact okay for you to get access to, and if you can have them, it will send them over, really the whole front-end and back-end communication thing is very key to this
-
HOW TO USE PY-COIN-GECKO to get data
-
-
You install the library using the
pippackage manager for python, at which point you import the library into your given code base, create an object representative of the API endpoint, which you can then query by something like ID, the currency type, and the days you’d like -
The above hand code gives you the data for bitcoin over the past 30 days
-
The output looks like:
-
It is originally in just a given response and then cleaned up to be a more readable dataframe
-
-
This is basically a json file that is a python dictionary of nested python lists, and it includes things like price, market cap, and total volumes
-
But this is really great because it gives you a TON of possible data to mess around and word with using other things like Pandas or Numpy so that way you can see if there are any general trends within of the bitcoin price or status over the past month
-
==REST APIs, Web Scraping, and Working with Files
-
REST APIs & HTTP Requests - Part 1:
- HTTP Protocol- this can be thought of as a general protocol of transferring information through the web, which includes a ton of different Rest APIs, which generally transfer data through a JSON file
- When you as a client use a web-page, your browser sends out a request over to the local that the web server is hosting the HTML code for the website, the server then sends back the HTML object back to the client in an HTTP response, which is what loads the web-page
- For the actual TCP/IP protocol and 5 or 7 layered communication that allows the sending of these packets, see Bits and Bytes of Computer Networking
- This generally is not only used for sending HTML content though, but is used for sending almost all of the data between these two endpoints, letting you send things over like the things that a User would input, or data from a database that they would want to query
- Uniform Resource Locator (URL) This is basically the most popular way of finding things over on the web, it can be broken down into three parts
- Scheme, like
http or httpsand is the protocol that is used to send information over for each of these requests on the application layer - Internet Address, Domain-name, or Base URL which is the actual domain of the website like
midascloud.netor something like that - Route- this is the location on the web server for a specific page or whatever content you want to access, so something like
portal.midascloud.net/registeror something like that - Once again, check out Bits and Bytes of Computer Networking for more on things like DNS, how a given url leads to a given IP address, and more
- REQUEST AND RESPONSE PROCESS
- This is an example of a request message, the start line is the GET method, and it is requesting the
index.htmlfile with addition information that this is an HTTP request - You can have different request types like POST, GET, or DELETE or some shit like that, once again check out IBM Developing Back-End Applications with Node for more
- The request header has addition information with the request, basically I guess the things that it can do with it or something like that? Maybe the type of data that is actually in there? I am not too sure
- There is also sometimes an additional body portion of the request, which is sometimes the actual data that is sent back
- This is basically what the response would be, the body is the actual
index.htmlfile from the server, and the response is that there was in fact a 200 status code that ended up working out fine - There is also extra information inside of the Response Header that says this is an apache server
- There’s a bunch of different status codes, some more in table, I’ve covered them a lot
-
REST APIs & HTTP Requests - Part 2:
- Request Modules In Python, is basically just something that allows you to send over HTTP requests using the python programming language
- The above hand is basically how you can make a pretty easy and simple request over to a given URL using this
requestslibrary - First you do the standard
import rquestsas long as you have it installed inside of your environment - You can then get whatever URL you would like, and make a request using
r=requests.get(URL)which creates a given object that holds all of the request data - Using this data you can get things like the body, the response status, and even the headers, allowing you to also actually get back the headers as a dictionary, with the attribute for the header, and then value it holds
- Following from the above hand example, you can then obviously access this dictionary using Keys for specific attributes that you might be interested in
- This lets you get specific data off of that website, and then you can check a bunch of different things like the text encoding they use, or the actual HTML body
- Get Requests with URL Parameters
- You can basically change the route of whatever URL you are trying to get data from to access the data at that given point, so if you have something special like
/api/getDataHereyou can query that using the URL and get the data you would like - After the given
/getexample of the image, you also have something called the query string which is basically the actual specific things that you want to get for that GET request - This is the actual part of the URL and sends over extra data to the web server so that way the server can fetch the exact data that you are looking for
- This makes it good for wanting to only web scrape specific portions of data, like say artist’s music that were only made in 1980 or something like that, for whatever purpose you want
- The start of the query is a
?and then followed by the data you want to access, which is conjoined with a&, so something like? Name=Joseph&ID=123 - This is made very easy inside of something like
requestswhere you can basically just have your standard URL with whatever extra route you want to get, and I really think this in conjunction with something like Traceroute or whatever the fuck that scrapes the extra routes on a site would be brutal - But anywho, you do that, have the payload information you want in a given dictionary, and then you actually just toss that into the
getfunction and it just formats it automatically for you which is sweet - You can then get the actual
robject and check out the request and response data that you got back - Allowing you to streamline that bitch right on over using pandas or numpy
-
Web Scraping and HTML Basics:
- Needs Text
-
HTML For Web Scraping:
-
Web Pages basically hold a whole bunch of really good data that you might want to access to for whatever nefarious purposes you might end up thinking of
-
Whether it be dogging out the fuck out of Zillow and their real-estate prices, or stack overflow for their coding questions
-
You might want to ping out a url request, get the HTML body, and then scrape it for given contents based on your specifications and the things you want
-
This is called Web Scraping
-
See Common HTML Elements for basically things like the DOM tree, the structure of HTML, the different tags and the different attributes
-
Feel free to also check out IBM - Developing Front-End Apps with React and IBM Introduction to HTML, CSS, & JavaScript to know a little bit more about front end and HTML as well
-
-
Say you want to get the text from the given tags and elements inside of each of these elements
-
This way you can basically get the free information about the salary of top NBA players
-
General, notice that something like the
h3elements hold the names, this would come in handy to know if it is repeated through a large source of data, because then you can automate the pipeline of cleaning up what you scrape -
You can check out the structure of a page that you might want to scrape by inspecting the HTML on a given page using something like
ctrl Ior whatever it is to get to the consolefURR
-
-
Web Scraping:
-
Web Scraping is a process that can be used to automatically extract data from a website, and can be very easily done using python and the
requestandBeautiful Souplibraries -
Web scraping is used in various fields and has many applications:
- Price Comparison: Services such as Parse-hub use web scraping to collect data from online shopping websites and use it to compare the prices of products.
- Email address gathering: Many companies that use email as a medium for marketing, use web scraping to collect email ID and then send bulk emails.
- Social Media Scraping: Web scraping is used to collect data from Social Media websites such as Twitter to find out what’s trending.
-
Scraping Example:
-
-
Given the website content that is displayed in the image, how would you get the output that you want inside of a read-able format
-
-
You first do
from bs4 import Beautiful Soup, in which you can store the html web-page content inside of whatever variable it is that you want to use -
To then parse the document, you can use
soup = BeautifulSoup(html, 'html5lib) -
This then gives you a Beautiful Soup Object which allows you to represent the given html string as an actual tree structure
-
-
You can then use things like
soup.titleor whatever other tag object in order to access that exact object -
If there is more than one tag with that exact name or something like that, then the first object that comes up in the tree structure is the one that would come up
-
-
-
To then get a direct object you would want, you can navigate up or down a given tree using common sense and good old logic as you see in the code example above
-
You can also use the
parentattribute to navigate your way up the tree as well, instead of just having specific child elements -
If you want to then have some other next object, on the parent object or whatever the fuck, you can call the
.next_siblingobject in order to see what that one might be -
-
If you want the actual string that is held inside of the element that you have directed your way into, then you can do that using the
.attrsfor whatever object you do it for. This would give you the dictionary of the attribute and whatever value it has -
If you want the actual string content inside, you can do
.string, which basically makes it into something similar to a python string -
-
-
FIND_ALL really
find_all -
Which is a filter that lets you scrape the entire document and lets you deciede what you want to filter on, that being something like the actual element, the attributes, or even the string inside of that element
-
This is obviously super useful for finding out bulks of information without a lot of manual coding
-
The
.find_allmethod basically looks through ALL of the children of a given parent object and locates all of the data that you want it to locate with, giving it to you in basically a list format, it’s more special than a list but it acts like a python list
-






















