Answers for "find all elemetns with class beautifulsoup"

2

find element in beautifulsoup by partial attribute value

# Use regex

# suppose I have list of 'div' with 'class' as follows:
# <div class='abcd bcde cdef efgh'>some content</div>
# <div class='mnop bcde cdef efgh'>some content</div>
# <div class='abcd pqrs cdef efgh'>some content</div>
# <div class='hijk wxyz cdef efgh'>some content</div>

# 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(<your_html_response>, <parser_you_want_to_use>)
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/
Posted by: Guest on December-03-2020
5

beautifulsoup find by class

soup.find_all("a", class_="sister")
Posted by: Guest on May-12-2020

Code answers related to "find all elemetns with class beautifulsoup"

Python Answers by Framework

Browse Popular Code Answers by Language