0% found this document useful (0 votes)
31 views4 pages

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

The document provides a cheat sheet on various Python methods and functions for working with APIs and scraping data from web pages. It includes code snippets and descriptions of common requests methods like GET, POST, PUT, DELETE as well as BeautifulSoup functions for parsing HTML like find, find_all, and methods for interacting with elements in the DOM like parent and find_next_sibling.

Uploaded by

SAIFUR RAHMAN
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)
31 views4 pages

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

The document provides a cheat sheet on various Python methods and functions for working with APIs and scraping data from web pages. It includes code snippets and descriptions of common requests methods like GET, POST, PUT, DELETE as well as BeautifulSoup functions for parsing HTML like find, find_all, and methods for interacting with elements in the DOM like parent and find_next_sibling.

Uploaded by

SAIFUR RAHMAN
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

2/10/24, 11:09 PM 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!
findChildren() Find all child Syntax:
elements of an
1. 1
HTML
element. 1. children = element.findChildren()

Copied!

Example:

1. 1

about:blank 1/4
2/10/24, 11:09 PM about:blank
1. child_elements = parent_div.findChildren()

Copied!
Perform a
GET request
to retrieve data
from a Syntax:
specified
1. 1
URL. GET
requests are 1. response = requests.get(url)
typically used
for reading Copied!
get() data from an
API. The Example:
response
variable will 1. 1
contain the 1. response = requests.get((https://api.example.com/data))
server's
response, Copied!
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 = requests.get(bas
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!
parent Access the Syntax:
parent element
1. 1
in the
Document 1. parent = element.parent
Object Model
(DOM). Copied!

Example:

about:blank 2/4
2/10/24, 11:09 PM about:blank
1. 1

1. parent_div = paragraph.parent

Copied!

Send a POST
request to a Syntax:
specified URL
1. 1
with data.
Create or 1. response = requests.post(url, data)
update POST
requests using Copied!
post() resources on
the server. The Example:
data parameter
contains the 1. 1
data to send to 1. response = requests.post((https://api.example.com/submit), data={(key): (value)})
the server,
often in JSON Copied!
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!
status_code Check the Syntax:
HTTP status
1. 1
code of the
response. The 1. response.status_code
HTTP status
code indicates Copied!
the result of
the request Example:
(success, error,
redirection). 1. 1
2. 2
Use the HTTP 3. 3
status codeIt
can be used for 1. url = "https://api.example.com/data"
error handling 2. response = requests.get(url)
3. status_code = response.status_code
and decision-

about:blank 3/4
2/10/24, 11:09 PM about:blank
making in Copied!
your code.
Tag Example:
1. 1
2. 2
3. 3
Specify any 4. 4
valid HTML 5. 5
tag as the tag 6. 6
7. 7
parameter to
8. 8
search for 9. 9
elements of 10. 10
tags for find()
that type. Here
and find_all() 1. - (a): Find anchor () tags.
are some
2. - (p): Find paragraph ((p)) tags.
common 3. - (h1), (h2), (h3), (h4), (h5), (h6): Find heading tags from level 1 to 6 ( (h1),n (h2)).
HTML tags 4. - (table): Find table () tags.
that you can 5. - (tr): Find table row () tags.
use with the 6. - (td): Find table cell ((td)) tags.
tag parameter. 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