Answers for "bf soup find() text parameter return string"

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
0

beautifulsoup find by text

soup.find_all("a", string="Elsie")
# [<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>]
Posted by: Guest on September-17-2020

Python Answers by Framework

Browse Popular Code Answers by Language