beautiful soup find text code example

Example 1: beautifulsoup find all class

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

Example 2: beautifulsoup get text

# Find all of the text between paragraph tags and strip out the html
page = soup.find('p').getText()

Example 3: beautifulsoup find by text

soup.find_all("a", string="Elsie")
# [<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>]

Example 4: python beautifulsoup get attibute

xmlData = None

with open('conf//test1.xml', 'r') as xmlFile:
    xmlData = xmlFile.read()

xmlDecoded = xmlData

xmlSoup = BeautifulSoup(xmlData, 'html.parser')

repElemList = xmlSoup.find_all('repeatingelement')

for repElem in repElemList:
    print("Processing repElem...")
    repElemID = repElem.get('id')
    repElemName = repElem.get('name')

    print("Attribute id = %s" % repElemID)
    print("Attribute name = %s" % repElemName)