How to parse class in one style BeautifulSoup4 python?

Question:

I have the following html:

<tr style="height: 45px"><td class="s32" dir="ltr">100</td></tr>
<tr style="height: 35px"><td class="s32" dir="ltr">1000</td></tr>

I need to parse 's32' class only in 'height: 45px' style but programm parse all 's32' class

I recently start use bs4 and I still don’t understand how to parse something in one style, can someone explain me that, please?

Now, I have that code:

import requests
from bs4 import BeautifulSoup as b

url = ''
r = requests.get(url)
soup = b(r.text, 'html.parser')
output = soup.find_all('td', class_='s32')
clear_output = [c.text for c in output]
print(clear_output)

Output:

['100', '1000']
Asked By: kgjfdhvd

||

Answers:

Since the style is on the parent, you’ll need to find_all td, then check if the .parent['style'] includes something like height: 45px, if so, add to a list:

import requests
from bs4 import BeautifulSoup

html = """
    <tr style="height: 45px"><td class="s32" dir="ltr">100</td></tr>
    <tr style="height: 35px"><td class="s32" dir="ltr">1000</td></tr>
"""

result = []

soup = BeautifulSoup(html, 'html.parser')
tds = soup.find_all('td', class_='s32')

for td in tds:
    if 'height: 45px' in td.parent['style']:
        result.append(td.getText())

print(result)

Will output:

['100']
Answered By: 0stone0
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.