Issue
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']
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.