Cheat Sheet: API's and Data Collection: Package/Method Description Code Example
Cheat Sheet: API's and Data Collection: Package/Method Description Code Example
about:blank
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
15/5/25, 11:48 a.m. about:blank
specified tag
and attributes.
Example:
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()
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).
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.
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
about:blank 6/6