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

Cheat Sheet: API's and Data Collection: Package/Method Description Code Example

This document is a cheat sheet for APIs and data collection, detailing various methods and their syntax for web scraping and API interactions. It covers functions such as accessing HTML attributes, sending HTTP requests (GET, POST, DELETE, PUT), and parsing responses with BeautifulSoup and JSON. Code examples are provided for each method to illustrate their usage in Python.

Uploaded by

Edwin Pimiento
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)
4 views6 pages

Cheat Sheet: API's and Data Collection: Package/Method Description Code Example

This document is a cheat sheet for APIs and data collection, detailing various methods and their syntax for web scraping and API interactions. It covers functions such as accessing HTML attributes, sending HTTP requests (GET, POST, DELETE, PUT), and parsing responses with BeautifulSoup and JSON. Code examples are provided for each method to illustrate their usage in Python.

Uploaded by

Edwin Pimiento
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

15/5/25, 11:48 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.

find() Find the first Syntax:


HTML
element = soup.find(tag, attrs)
element that
matches the

about:blank 1/6
15/5/25, 11:48 a.m. about:blank
specified tag
and attributes.

Example:

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 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
about:blank 2/6
15/5/25, 11:48 a.m. about:blank
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 = re
such as
authentication
tokens or
content types.

Syntax:
from bs4 import BeautifulSoup
Import the
necessary
Import Libraries Python
libraries for
web scraping.

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

next_sibling() Find the next Syntax:


sibling
sibling = element.find_next_sibling()
element in the
DOM.

Example:

about:blank 3/6
15/5/25, 11:48 a.m. about:blank
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.

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

Send a PUT
request to
update data on
the server.
PUT requests
are used to
update an
existing
put()
resource on Example:
the server with
the data response = requests.put((https://api.example.com/update), data={(key): (value)})
provided in the
data
parameter,
typically in
JSON format.

about:blank 4/6
15/5/25, 11:48 a.m. about:blank

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"
conditions or params = {"page": 1, "per_page": 10}
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))

Syntax:
response.status_code

Check the
HTTP status
code of the
response. The
HTTP status
code indicates
the result of
the request
status_code (success, error, Example:
redirection).
Use the HTTP url = "https://api.example.com/data"
status codeIt response = requests.get(url)
status_code = response.status_code
can be used for
error handling
and decision-
making in
your code.

tags for find() Specify any Tag Example:


and find_all() valid HTML
- (a): Find anchor () tags.
tag as the tag
- (p): Find paragraph ((p)) tags.
parameter to - (h1), (h2), (h3), (h4), (h5), (h6): Find heading tags from level 1 to 6 ( (h1),n (h2)).
search for - (table): Find table () tags.
elements of - (tr): Find table row () tags.
that type. Here - (td): Find table cell ((td)) tags.

about:blank 5/6
15/5/25, 11:48 a.m. about:blank
are some - (th): Find table header cell ((td))tags.
common - (img): Find image ((img)) tags.
- (form): Find form ((form)) tags.
HTML tags - (button): Find button ((button)) 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