Home > Back-end >  Find with text using python and regex BeautifulSoup lxml
Find with text using python and regex BeautifulSoup lxml

Time:11-12

how to find data with text ? for example

<td>
    <span class="data-list clear-none">active</span>
    <span class="data-list clear-none">not active</span>
    <span class="data-list clear-none">null</span>
    <span class="data-list clear-none">none</span>
<td>

i want to only get class which contain active ? what is best way? now to try

current_lifter = soup.findAll("span", {"class": "data-list clear-none"})

but want to get only text which start active

CodePudding user response:

Use string='active' as parameter of find (or findAll)

>>> soup.find("span", {"class": "data-list clear-none"}, string='active')
<span class="data-list clear-none">active</span>

Is there any fucntion for example active *. I mean get eveything after active

import re

soup.find("span", {"class": "data-list clear-none"}, string=re.compile('^active.*'))

Refer to the documentation. string can also accept regular expression.

CodePudding user response:

current_lifter = soup.find_all("span", {class = "data-list clear-none"}, string='active')

Then run the iterator to get the text.

  • Related