0

I'm parsing a html with beautifulsoup. I need to check if a tag have style with something like border.*:.*px.

I can find all the tags with style by,

soup.find_all(["tr"],style=re.compile(r'border.*:[^:]*px'))

But I've to traverse the html in order, so for a tag, how can I check if it has the style of r'border.*:[^:]*px'.

I've also refer to Test if an attribute is present in a tag in BeautifulSoup, use the has_attr method of tag, but is seems it not support regular.

value = re.compile(r'border.*:[^:]*px')
tag.has_attr("{'style':"+value+"}")

but it shows

TypeError                                 Traceback (most recent call last)
<ipython-input-202-1e077ea6ea4c> in <module>
      1 value = re.compile(r'border.*:[^:]*px')
----> 2 tag.has_attr("{'style':"+value+"}")

TypeError: must be str, not _sre.SRE_Pattern
LF00
  • 27,015
  • 29
  • 156
  • 295
  • Check this out? There are 3 different ways to do this hopefully [Beautiful Soup Tag Style](https://stackoverflow.com/a/35140202/11700321) – EGC Oct 23 '19 at 02:49
  • @EGC Thank you again. The link is about how to find tags with some style. My question is about to check if the tag has some style. – LF00 Oct 23 '19 at 02:51
  • @EGC In my question I've said I know how to find tags with some style. – LF00 Oct 23 '19 at 02:52

2 Answers2

1
def foo(tag):
    import re
    tag_style = tag.attrs.get('style')
    return bool(re.search(r'border.*:[^:]*px', tag_style)) if tag_style else False
Sir. MaNiAl
  • 164
  • 1
  • 6
0

I didn't find a method of BeautifulSoup4 to get it. So I use re module as a workaround.

import re
border_re = re.compile(r'border.*:[^:]*px')

if tag.has_attr('style') and border_re.search(tag.attrs['style']):
LF00
  • 27,015
  • 29
  • 156
  • 295