0% found this document useful (0 votes)
22 views6 pages

CheatSheet - APIs and Data Collection

This document serves as a cheat sheet for using APIs and data collection methods in Python, detailing various functions and their syntax for web scraping and making HTTP requests. Key functions include accessing HTML attributes, sending GET, POST, PUT, and DELETE requests, and parsing JSON data. It also covers the use of BeautifulSoup for HTML parsing and finding elements within the document structure.

Uploaded by

chispitaboom53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

CheatSheet - APIs and Data Collection

This document serves as a cheat sheet for using APIs and data collection methods in Python, detailing various functions and their syntax for web scraping and making HTTP requests. Key functions include accessing HTML attributes, sending GET, POST, PUT, and DELETE requests, and parsing JSON data. It also covers the use of BeautifulSoup for HTML parsing and finding elements within the document structure.

Uploaded by

chispitaboom53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11/8/25, 11:50 a.m.

about:blank

Cheat Sheet: API's and Data Collection


Package/Method Description Code Example

Syntax:
attribute = element[(attribute)]

Access the
value of a
Accessing specific
element attribute attribute of an Example:
HTML
element. href = link_element[(href)]

Syntax:
soup = BeautifulSoup(html, (html.parser))

Parse the
HTML content
of a web page
using
BeautifulSoup() BeautifulSoup.
The parser Example:
type can vary
html = (https://api.example.com/data) soup = BeautifulSoup(html, (html.parser))
based on the
project.

Syntax:
response = requests.delete(url)

Send a
DELETE
request to
remove data or
a resource
from the
delete()
server. Example:
DELETE
requests delete response = requests.delete((https://api.example.com/delete))
a specified
resource on
the server.

about:blank 1/6
11/8/25, 11:50 a.m. about:blank

Syntax:
element = soup.find(tag, attrs)

Find the first


HTML
element that
find()
matches the Example:
specified tag
and attributes. first_link = soup.find((a), {(class): (link)})

Syntax:
elements = soup.find_all(tag, attrs)

Find all
HTML
elements that
find_all()
match the Example:
specified tag
and attributes. all_links = soup.find_all((a), {(class): (link)})</td>

Syntax:
children = element.findChildren()

Find all child


elements of an
findChildren()
HTML Example:
element.
child_elements = parent_div.findChildren()

get() Perform a Syntax:


GET request
response = requests.get(url)
to retrieve data
from a
specified

about:blank 2/6
11/8/25, 11:50 a.m. about:blank
URL. GET
requests are
typically used
for reading
data from an
API. The
response
variable will
contain the Example:
server's response = requests.get((https://api.example.com/data))
response,
which you can
process
further.

Syntax:
headers = {(HeaderName): (Value)}

Include
custom
headers in the
request.
Headers can
provide
Headers additional
information to Example:
the server,
base_url = (https://api.example.com/data) headers = {(Authorization): (Bearer YOUR_TOKEN)} response = requests.ge
such as
authentication
tokens or
content types.

Syntax:
from bs4 import BeautifulSoup

Import the
necessary
Import Libraries Python
libraries for
web scraping.

json() Parse JSON Syntax:


data from the
data = response.json()
response. This
extracts and
works with the
data returned
by the API.
The
response.json()
method
converts the
JSON
response into a
Example:
Python data
structure response = requests.get((https://api.example.com/data))
(usually a data = response.json()
dictionary or
list).

about:blank 3/6
11/8/25, 11:50 a.m. about:blank

Syntax:
sibling = element.find_next_sibling()

Find the next


sibling
next_sibling()
element in the Example:
DOM.
next_sibling = current_element.find_next_sibling()

Syntax:
parent = element.parent

Access the
parent element
in the
parent
Document Example:
Object Model
(DOM). parent_div = paragraph.parent

Syntax:
response = requests.post(url, data)

Send a POST
request to a
specified URL
with data.
Create or
update POST
requests using
post() resources on
the server. The Example:
data parameter
response = requests.post((https://api.example.com/submit), data={(key): (value)})
contains the
data to send to
the server,
often in JSON
format.

put() Send a PUT Syntax:


request to
response = requests.put(url, data)
update data on

about:blank 4/6
11/8/25, 11:50 a.m. about:blank
the server.
PUT requests
are used to
update an
existing
resource on
the server with
the data
provided in the
data Example:
parameter,
typically in response = requests.put((https://api.example.com/update), data={(key): (value)})
JSON format.

Syntax:
params = {(param_name): (value)}

Pass query
parameters in
the URL to
filter or
customize the
Query parameters request. Query Example:
parameters
specify base_url = "https://api.example.com/data"
params = {"page": 1, "per_page": 10}
conditions or response = requests.get(base_url, params=params)
limits for the
requested data.

Syntax:
element = soup.select(selector)

Select HTML
elements from
select() the parsed
HTML using a Example:
CSS selector.
titles = soup.select((h1))

status_code Check the Syntax:


HTTP status
response.status_code
code of the
response. The
HTTP status
code indicates
the result of

about:blank 5/6
11/8/25, 11:50 a.m. about:blank
the request
(success, error,
redirection).
Use the HTTP
status codeIt
can be used for Example:
error handling
and decision- url = "https://api.example.com/data"
making in response = requests.get(url)
status_code = response.status_code
your code.

Tag Example:
- (a): Find anchor () tags.
- (p): Find paragraph ((p)) tags.
Specify any - (h1), (h2), (h3), (h4), (h5), (h6): Find heading tags from level 1 to 6 ( (h1),n (h2)).
valid HTML - (table): Find table () tags.
tag as the tag - (tr): Find table row () tags.
parameter to - (td): Find table cell ((td)) tags.
- (th): Find table header cell ((td))tags.
search for - (img): Find image ((img)) tags.
elements of - (form): Find form ((form)) tags.
tags for find()
that type. Here - (button): Find button ((button)) tags.
and find_all()
are some
common
HTML tags
that you can
use with the
tag parameter.

Syntax:
text = element.text

Retrieve the
text content of
text
an HTML Example:
element.
title_text = title_element.text

© IBM Corporation. All rights reserved.

about:blank 6/6

You might also like