0% found this document useful (0 votes)
37 views

API Cheatsheet

Uploaded by

Aashish Kumar
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)
37 views

API Cheatsheet

Uploaded by

Aashish Kumar
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/ 4

14/03/2024, 18:51 about:blank

Cheat Sheet: API's and Data Collection


Package/Method Description Code Example
Syntax:
1. 1

1. attribute = element[(attribute)]
Access the
value of a Copied!
Accessing specific
element attribute attribute of an 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 parser Example:
type can vary
1. 1
based on the
project. 1. html = (https://api.example.com/data) soup = BeautifulSoup(html, (html.parser))

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

1. element = soup.find(tag, attrs)


Find the first
HTML Copied!
element that
find()
matches the Example:
specified tag
and 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 Copied!
elements that
find_all()
match the Example:
specified tag
and attributes. 1. 1

1. all_links = soup.find_all((a), {(class): (link)})</td>

Copied!
Syntax:
1. 1

1. children = element.findChildren()
Find all child Copied!
elements of an
findChildren()
HTML Example:
element.
1. 1

1. child_elements = parent_div.findChildren()

Copied!
get() Perform a Syntax:
GET request
1. 1

about:blank 1/4
14/03/2024, 18:51 about:blank
to retrieve data 1. response = requests.get(url)
from a
specified URL. Copied!
GET requests
are typically Example:
used for 1. 1
reading data
from an API. 1. response = requests.get((https://api.example.com/data))
The response
variable will Copied!
contain the
server's
response,
which you can
process
further.
Include Syntax:
custom
1. 1
headers in the
request. 1. headers = {(HeaderName): (Value)}
Headers can
provide Copied!
Headers additional
information to Example:
the server,
1. 1
such as
authentication 1. base_url = (https://api.example.com/data) headers = {(Authorization): (Bearer YOUR_TOKEN)} response = re
tokens or
content types. Copied!
Syntax:
Import the
necessary 1. 1
Import Libraries Python
libraries for 1. from bs4 import BeautifulSoup
web scraping. Copied!
Parse JSON
data from the
response. This Syntax:
extracts and 1. 1
works with the
data returned 1. data = response.json()
by the API.
Copied!
The
response.json()
json() Example:
method
converts the 1. 1
JSON 2. 2
response into a
Python data 1. response = requests.get((https://api.example.com/data))
2. data = response.json()
structure
(usually a Copied!
dictionary or
list).
Syntax:
1. 1

1. sibling = element.find_next_sibling()
Find the next Copied!
sibling
next_sibling()
element in the Example:
DOM.
1. 1

1. next_sibling = current_element.find_next_sibling()

Copied!
Syntax:
1. 1

1. parent = element.parent
Access the
parent element Copied!
in the
parent
Document Example:
Object Model
(DOM). 1. 1

1. parent_div = paragraph.parent

Copied!
post() Send a POST Syntax:
request to a
1. 1
specified URL
with data. 1. response = requests.post(url, data)
Create or
update POST Copied!

about:blank 2/4
14/03/2024, 18:51 about:blank
requests using Example:
resources on
the server. The 1. 1
data parameter 1. response = requests.post((https://api.example.com/submit), data={(key): (value)})
contains the
data to send to Copied!
the server,
often in JSON
format.
Send a PUT
request to
Syntax:
update data on
the server. 1. 1
PUT requests
are used to 1. response = requests.put(url, data)
update an Copied!
existing
put()
resource on Example:
the server with
the data 1. 1
provided in the
data 1. response = requests.put((https://api.example.com/update), data={(key): (value)})
parameter, Copied!
typically in
JSON format.
Syntax:
1. 1
Pass query
1. params = {(param_name): (value)}
parameters in
the URL to Copied!
filter or
customize the Example:
Query parameters request. Query
parameters 1. 1
specify 2. 2
3. 3
conditions or
limits for the 1. base_url = "https://api.example.com/data"
requested data. 2. params = {"page": 1, "per_page": 10}
3. response = requests.get(base_url, params=params)

Copied!
Syntax:
1. 1

1. element = soup.select(selector)
Select HTML
elements from Copied!
select() the parsed
HTML using a Example:
CSS selector.
1. 1

1. titles = soup.select((h1))

Copied!
Check the Syntax:
HTTP status
code of the 1. 1
response. The
1. response.status_code
HTTP status
code indicates Copied!
the result of
the request Example:
status_code (success, error,
redirection). 1. 1
Use the HTTP 2. 2
status codeIt 3. 3
can be used for 1. url = "https://api.example.com/data"
error handling 2. response = requests.get(url)
and decision- 3. status_code = response.status_code
making in
Copied!
your code.
tags for find() Specify any Tag Example:
and find_all() valid HTML
1. 1
tag as the tag 2. 2
parameter to 3. 3
search for 4. 4
elements of 5. 5
that type. Here 6. 6
7. 7
are some 8. 8
common 9. 9
HTML tags 10. 10
that you can
use with the 1. - (a): Find anchor () tags.
2. - (p): Find paragraph ((p)) tags.
tag parameter. 3. - (h1), (h2), (h3), (h4), (h5), (h6): Find heading tags from level 1 to 6 ( (h1),n (h2)).
4. - (table): Find table () tags.

about:blank 3/4
14/03/2024, 18:51 about:blank
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 Copied!
text content of
text
an HTML Example:
element.
1. 1

1. title_text = title_element.text

Copied!

© IBM Corporation. All rights reserved.

about:blank 4/4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy