Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 测试标记在BeautifulSoup中是否具有样式_Python_Html_Beautifulsoup_Tags_Styles - Fatal编程技术网

Python 测试标记在BeautifulSoup中是否具有样式

Python 测试标记在BeautifulSoup中是否具有样式,python,html,beautifulsoup,tags,styles,Python,Html,Beautifulsoup,Tags,Styles,我正在用beautifulsoup解析html。我需要检查标记是否具有类似于边框的样式。*:.*px 我可以通过找到所有带有样式的标签 soup.find_all(["tr"],style=re.compile(r'border.*:[^:]*px')) 但是我必须按顺序遍历html,因此对于标记,我如何检查它是否具有r'border.*:[^:]*px'的样式 我也提到了,使用标签的has_attr方法,但似乎它不支持正则表达式 value = re.compile(r'border.*:[

我正在用beautifulsoup解析html。我需要检查标记是否具有类似于
边框的样式。*:.*px

我可以通过找到所有带有样式的标签

soup.find_all(["tr"],style=re.compile(r'border.*:[^:]*px'))
但是我必须按顺序遍历html,因此对于标记,我如何检查它是否具有
r'border.*:[^:]*px'
的样式

我也提到了,使用标签的
has_attr
方法,但似乎它不支持正则表达式

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

TypeError回溯(最近一次调用)
在里面
1值=重新编译(r'border.*:[^:]*px')
---->2 tag.has_attr(“{'style':“+value+”}”)
TypeError:必须是str,而不是_sre.sre_模式
def foo(标签):
进口稀土
tag\u style=tag.attrs.get('style'))
如果tag_样式为False,则返回bool(重新搜索(r'border.*:[^:]*px',tag_样式))

我没有找到一种方法来获得它。因此,我使用
re
模块作为解决方法

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

if tag.has_attr('style') and border_re.search(tag.attrs['style']):

看看这个?有三种不同的方法可以做到这一点,希望@EGC再次感谢你。该链接是关于如何查找具有某种样式的标记的。我的问题是要检查标记是否有某种样式。@EGC在我的问题中,我说过我知道如何找到具有某种样式的标记。
import re
border_re = re.compile(r'border.*:[^:]*px')

if tag.has_attr('style') and border_re.search(tag.attrs['style']):