Using bs4 I need to find an element with class_=re.compile("viewLicense") but not class_="viewLicenseDetails"
Here is the snippet,
<tr class="viewLicense inactive"></tr>
<tr class="viewLicense"></tr>
<tr id="licenseDetails_552738" class="viewLicenseDetails"</tr>
I want the first two tr and not want the last one.
Could someone please help, Thanks
Following will find every tr tag with viewLicense
soup.find_all("tr", class_="viewLicense")
So, it will work for the text provided in quesiton:
>>> soup.find_all("tr", class_="viewLicense")
[<tr class="viewLicense inactive"></tr>, <tr class="viewLicense"></tr>]
However if you have a tr tag which has both viewLicense and viewLicenseDetails classes, then following will find all tr tags with viewLicense and then remove tags with viewLicenseDetails:
>>> both_tags = soup.find_all("tr", class_="viewLicense")
>>> for tag in both_tags:
... if 'viewLicenseDetails' not in tag.attrs['class']:
... print tag
Use CSS selectors?
results = soup.select('tr.viewLicense')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With