Using BeautifulSoup to find tag with two specific styles

前端 未结 1 329
轻奢々
轻奢々 2020-12-16 07:50

I am trying to use the BeautifulSoup (bs4) package in Python2.7 to find the following tag in an html document:

相关标签:
1条回答
  • 2020-12-16 08:24

    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.

    0 讨论(0)
提交回复
热议问题