Beautifulsoup Match Empty Class

Viewed 469

I am scraping a table on a website where I am only trying to return any row where the class is blank (Row 1 and 4)

<tr class>Row 1</tr>
<tr class="is-oos ">Row 2</tr>
<tr class="somethingelse">Row 3</tr>
<tr class>Row 4</tr>

(Note there is a trailing space at the end of the is-oos class.

When I do soup.findAll('tr', class_=None) it matches all the rows. This is because Row 2 has the class ['is-oos', ''] due to the trailing space. Is there a simple way to do a soup.findAll() or soup.select() to match these rows?

1 Answers

Try class_="":

from bs4 import BeautifulSoup

html_doc = """<tr class>Row 1</tr>
<tr class="is-oos ">Row 2</tr>
<tr class="somethingelse">Row 3</tr>
<tr class>Row 4</tr>"""

soup = BeautifulSoup(html_doc, "html.parser")

print(*soup.find_all('tr', class_=""))

# Or to only get the text
print( '\n'.join(t.text for t in soup.find_all('tr', class_="")) )

Outputs:

<tr class="">Row 1</tr> <tr class="">Row 4</tr>
Row 1
Row 4

Edit To only get what's in stock, we can check the attributes of the tag:

import requests
from bs4 import BeautifulSoup

URL = "https://gun.deals/search/apachesolr_search/736676037018"

soup = BeautifulSoup(requests.get(URL).text, "html.parser")

for tag in soup.find_all('tr'):
    if tag.attrs.get('class') == ['price-compare-table__oos-breaker', 'js-oos-breaker']:
        break
    print(tag.text.strip())
Related