Cheat Sheet: API's and Data Collection
Package/Method Description Code Example
Syntax:
1. 1
1. attribute = element[(attribute)]
Access the value of a
Accessing Copied!
specific attribute of an
element attribute Example:
HTML element.
1. 1
1. href = link_element[(href)]
Copied!
Syntax:
1. 1
Parse the HTML content 1. soup = BeautifulSoup(html, (html.parser))
of a web page using Copied!
BeautifulSoup() BeautifulSoup. The Example:
parser type can vary 1. 1
based on the project. 1. html = (https://api.example.com/data) soup =
BeautifulSoup(html, (html.parser))
Copied!
Syntax:
1. 1
Send a DELETE request
1. response = requests.delete(url)
to remove data or a
Copied!
resource from the
delete() Example:
server. DELETE requests
1. 1
delete a specified
1. response =
resource on the server.
requests.delete((https://api.example.com/delete))
Copied!
Syntax:
1. 1
Find the first HTML 1. element = soup.find(tag, attrs)
element that matches Copied!
find()
the specified tag and Example:
attributes. 1. 1
1. first_link = soup.find((a), {(class): (link)})
Copied!
Syntax:
1. 1
1. elements = soup.find_all(tag, attrs)
Find all HTML elements
Copied!
find_all() that match the specified
Example:
tag and attributes.
1. 1
1. all_links = soup.find_all((a), {(class): (link)})</td>
Copied!
findChildren() Find all child elements of Syntax:
an HTML element. 1. 1
1. children = element.findChildren()
Copied!
Example:
1. 1
1. child_elements = parent_div.findChildren()
Copied!
Perform a GET request
Syntax:
to retrieve data from a
1. 1
specified URL. GET
1. response = requests.get(url)
requests are typically
Copied!
used for reading data
get() Example:
from an API. The
1. 1
response variable will
1. response =
contain the server's
requests.get((https://api.example.com/data))
response, which you can
Copied!
process further.
Syntax:
1. 1
Include custom headers
1. headers = {(HeaderName): (Value)}
in the request. Headers
Copied!
can provide additional
Example:
Headers information to the
1. 1
server, such as
1. base_url = (https://api.example.com/data) headers =
authentication tokens or
{(Authorization): (Bearer YOUR_TOKEN)} response =
content types.
requests.get(base_url, headers=headers)
Copied!
Syntax:
Import the necessary
1. 1
Import Libraries Python libraries for web
1. from bs4 import BeautifulSoup
scraping.
Copied!
Syntax:
Parse JSON data from
1. 1
the response. This
1. data = response.json()
extracts and works with
Copied!
the data returned by the
Example:
API. The response.json()
json() 1. 1
method converts the
2. 2
JSON response into a
1. response =
Python data structure
requests.get((https://api.example.com/data))
(usually a dictionary or
2. data = response.json()
list).
Copied!
next_sibling() Find the next sibling Syntax:
element in the DOM. 1. 1
1. sibling = element.find_next_sibling()
Copied!
Example:
1. 1
1. next_sibling = current_element.find_next_sibling()
Copied!
Syntax:
1. 1
Access the parent 1. parent = element.parent
element in the Copied!
parent
Document Object Model Example:
(DOM). 1. 1
1. parent_div = paragraph.parent
Copied!
Syntax:
Send a POST request to a
1. 1
specified URL with data.
1. response = requests.post(url, data)
Create or update POST
Copied!
requests using resources
Example:
post() on the server. The data
1. 1
parameter contains the
1. response =
data to send to the
requests.post((https://api.example.com/submit),
server, often in JSON
data={(key): (value)})
format.
Copied!
Syntax:
Send a PUT request to
1. 1
update data on the
1. response = requests.put(url, data)
server. PUT requests are
Copied!
used to update an
Example:
put() existing resource on the
1. 1
server with the data
1. response =
provided in the data
requests.put((https://api.example.com/update),
parameter, typically in
data={(key): (value)})
JSON format.
Copied!
Syntax:
1. 1
1. params = {(param_name): (value)}
Pass query parameters
Copied!
in the URL to filter or
Example:
customize the request.
Query 1. 1
Query parameters
parameters 2. 2
specify conditions or
3. 3
limits for the requested
1. base_url = "https://api.example.com/data"
data.
2. params = {"page": 1, "per_page": 10}
3. response = requests.get(base_url, params=params)
Copied!
select() Select HTML elements Syntax:
from the parsed HTML 1. 1
using a CSS selector. 1. element = soup.select(selector)
Copied!
Example:
1. 1
1. titles = soup.select((h1))
Copied!
Syntax:
Check the HTTP status 1. 1
code of the response. 1. response.status_code
The HTTP status code Copied!
indicates the result of Example:
the request (success, 1. 1
status_code
error, redirection). Use 2. 2
the HTTP status codeIt 3. 3
can be used for error 1. url = "https://api.example.com/data"
handling and decision- 2. response = requests.get(url)
making in your code. 3. status_code = response.status_code
Copied!
Tag Example:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
Specify any valid HTML 8. 8
tag as the tag parameter 9. 9
to search for elements of 10. 10
tags for find() and
that type. Here are some 1. - (a): Find anchor () tags.
find_all()
common HTML tags that 2. - (p): Find paragraph ((p)) tags.
you can use with the tag 3. - (h1), (h2), (h3), (h4), (h5), (h6): Find heading tags
parameter. from level 1 to 6 ( (h1),n (h2)).
4. - (table): Find table () tags.
5. - (tr): Find table row () tags.
6. - (td): Find table cell ((td)) tags.
7. - (th): Find table header cell ((td))tags.
8. - (img): Find image ((img)) tags.
9. - (form): Find form ((form)) tags.
10. - (button): Find button ((button)) tags.
Copied!
Syntax:
1. 1
1. text = element.text
Retrieve the text content
text Copied!
of an HTML element.
Example:
1. 1
1. title_text = title_element.text