I am trying to use the BeautifulSoup (bs4) package in Python2.7 to find the following tag in an html document:
<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:408px; top:540px; width:14px; height:9px;"><span style="font-family: OEULZL+ArialMT; font-size:9px">0.00<br></span></div>
In the html document there are multiple other tags that are almost exactly identical - the only consistently difference is the "left:408px" and the "height:9px" attributes.
How can i find this tag using BeautifulSoup
?
Ive tried the following:
from bs4 import BeautifulSoup as bs
soup = bs("<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:408px; top:540px; width:14px; height:9px;"><span style="font-family: OEULZL+ArialMT; font-size:9px">0.00<br></span></div>", 'html.parser')
soup.find_all('div', style=('left:408px' and 'height:9px'))
soup.find_all('div', style=('left:408px') and style=('height:9px')) #doesn't like style being used twice
soup.find_all('div', {'left':'408px' and 'height':'9px'})
soup.find_all('div', {'left:408px'} and {'height:9px'})
soup.find_all('div', style={'left':'408px' and 'height':'9px'})
soup.find_all('div', style={'left:408px'} and {'height:9px'})
Any ideas?
You can check the style
to have left:408px
and height:9px
inside it:
soup.find('div', style=lambda value: value and 'left:408px' in value and 'height:9px' in value)
Or:
import re
soup.find('div', style=re.compile(r'left:408px.*?height:9px'))
Or:
soup.select_one('div[style*="408px"]')
Note that, in general, style properties are not reliable to use for locating elements. See if there is anything else - check the parent, sibling elements, or may be there is a corresponding label near the element.
Note that, a more appropriate CSS selector would be div[style*="left:408px"][style*="height:9px"]
, but because of the limited CSS selector support and this bug, it is not gonna work as is.
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