Skip to content

Instantly share code, notes, and snippets.

@aleenprd
Last active October 20, 2022 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleenprd/f69704edd71da2dba575b9b290853387 to your computer and use it in GitHub Desktop.
Save aleenprd/f69704edd71da2dba575b9b290853387 to your computer and use it in GitHub Desktop.
fetch_el_if_available
from typing import Union
def fetch_el_if_available(soup: BeautifulSoup, element_type: str, class_type: str) -> Union[str, None]:
"""Returns element text if found, otherwise returns None.
Args:
soup (BeautifulSoup): a b24 soup.
element_type (str): HTML type e.g. 'div'.
class_type (str): the class of the desired element.
Returns:
element (str): text inside element.
"""
element = soup.find(element_type, class_type)
if element is not None:
element = element.text
return element
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment