Beautiful Soup - Class contains 'a' and not contains 'b'

Md. Mohsin

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

avi

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Scrape data from multiple webpages using a .txt file that contains the URLs with Python and beautiful soup

From Dev

How to Rename a Class in Beautiful Soup

From Dev

Java : Hashcode of a Class A containing a Class B which contains A

From Dev

How to extract value from a class in beautiful Soup

From Dev

Python - Beautiful Soup - Class name exists twice

From Dev

How to extract value from a class in beautiful Soup

From Dev

Define what class contains

From Dev

ProGuard - unexpectedly contains class

From Dev

XPath Class is and Text Contains

From Dev

jQuery if class name contains

From Dev

Is there a contains class equivalent for css?

From Dev

Use of 'CONTAINS(Foo, "A") OR CONTAINS(Foo, "B") vs CONTAINS(Foo, '"A" OR "B"') in SQL Server FullText

From Dev

Use of 'CONTAINS(Foo, "A") OR CONTAINS(Foo, "B") vs CONTAINS(Foo, '"A" OR "B"') in SQL Server FullText

From Dev

If column A contains x AND column B contains y THEN add value

From Java

Check if an element contains a class in JavaScript?

From Dev

Which .dll contains HTMLElement Class?

From Dev

Contains Class Element Identifier for Protractor

From Dev

How to serialize a Class contains BitmapImage?

From Dev

What JAR contains the class MQSimpleConnectionManager

From Dev

Super a variable which contains a class?

From Dev

Using Beautiful Soup to get data from non-class section

From Dev

Accessing <li> element with no class id using Beautiful soup

From Java

Scraping using beautiful soup: find class to add information in pandas

From Java

Web scraping using Beautiful Soup, scrapping multiple elements without class

From Dev

Beautiful soup - capture all links with a certain class or text

From Dev

How to select a class of div inside of a div with beautiful soup?

From Dev

Beautiful Soup: Only target elements if a specific child has a certain class

From Dev

python beautiful soup get Chinese immediately after class tag

From Dev

beautiful soup extracting parent/sibling tr table class

Related Related

  1. 1

    Scrape data from multiple webpages using a .txt file that contains the URLs with Python and beautiful soup

  2. 2

    How to Rename a Class in Beautiful Soup

  3. 3

    Java : Hashcode of a Class A containing a Class B which contains A

  4. 4

    How to extract value from a class in beautiful Soup

  5. 5

    Python - Beautiful Soup - Class name exists twice

  6. 6

    How to extract value from a class in beautiful Soup

  7. 7

    Define what class contains

  8. 8

    ProGuard - unexpectedly contains class

  9. 9

    XPath Class is and Text Contains

  10. 10

    jQuery if class name contains

  11. 11

    Is there a contains class equivalent for css?

  12. 12

    Use of 'CONTAINS(Foo, "A") OR CONTAINS(Foo, "B") vs CONTAINS(Foo, '"A" OR "B"') in SQL Server FullText

  13. 13

    Use of 'CONTAINS(Foo, "A") OR CONTAINS(Foo, "B") vs CONTAINS(Foo, '"A" OR "B"') in SQL Server FullText

  14. 14

    If column A contains x AND column B contains y THEN add value

  15. 15

    Check if an element contains a class in JavaScript?

  16. 16

    Which .dll contains HTMLElement Class?

  17. 17

    Contains Class Element Identifier for Protractor

  18. 18

    How to serialize a Class contains BitmapImage?

  19. 19

    What JAR contains the class MQSimpleConnectionManager

  20. 20

    Super a variable which contains a class?

  21. 21

    Using Beautiful Soup to get data from non-class section

  22. 22

    Accessing <li> element with no class id using Beautiful soup

  23. 23

    Scraping using beautiful soup: find class to add information in pandas

  24. 24

    Web scraping using Beautiful Soup, scrapping multiple elements without class

  25. 25

    Beautiful soup - capture all links with a certain class or text

  26. 26

    How to select a class of div inside of a div with beautiful soup?

  27. 27

    Beautiful Soup: Only target elements if a specific child has a certain class

  28. 28

    python beautiful soup get Chinese immediately after class tag

  29. 29

    beautiful soup extracting parent/sibling tr table class

HotTag

Archive