beautifulsoup find element by tag code example

Example 1: find element in beautifulsoup by partial attribute value

# Use regex

# suppose I have list of 'div' with 'class' as follows:
# 
some content
#
some content
#
some content
#
some content
# as observable the class value string of above div(s) ends with 'cdef efgh' # So to extract all these in a single list: from bs4 import BeautifulSoup import re # library for regex in python soup = BeautifulSoup(, ) elements = soup.find_all('div', {'class': re.compile(r'cdef efgh$')}) # $ means that 'cdef efgh' must is the ending of the string # Note: This was just one case. You can make almost any case with regex. # Learn more and experiment with regex at https://regex101.com/

Example 2: beautifulsoup find class

mydivs = soup.findAll("div", {"class": "stylelistrow"})

Tags:

Misc Example