Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.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 解析嵌入的css美化组_Python_Html_Css_Beautifulsoup - Fatal编程技术网

Python 解析嵌入的css美化组

Python 解析嵌入的css美化组,python,html,css,beautifulsoup,Python,Html,Css,Beautifulsoup,是否可以从html标记中提取嵌入的css属性?例如,假设我想找出“s5”的垂直对齐属性是什么 我目前正在使用beautifulsoup,并已使用tag=soup.find(class=“s5”)检索span标记。我尝试了tag.attrs[“class”],但这只会给我s5,无法将其链接到嵌入式样式。在python中可以这样做吗?我发现的每一个此类问题都涉及到分析内联css样式 *{边距:0;填充:0;文本缩进:0;} .s5{颜色:#000;字体系列:Verdana,无衬线; 字体样式:普

是否可以从html标记中提取嵌入的css属性?例如,假设我想找出“s5”的垂直对齐属性是什么

我目前正在使用beautifulsoup,并已使用
tag=soup.find(class=“s5”)
检索span标记。我尝试了
tag.attrs[“class”]
,但这只会给我
s5
,无法将其链接到嵌入式样式。在python中可以这样做吗?我发现的每一个此类问题都涉及到分析内联css样式


*{边距:0;填充:0;文本缩进:0;}
.s5{颜色:#000;字体系列:Verdana,无衬线;
字体样式:普通;字体大小:普通;
文字装饰:无;字体大小:17.5pt;
垂直对齐:10pt;}

这是一个例句。1.


您可以使用类似的css解析器。我不知道包中是否有函数可以执行类似的操作(有人可以对此进行评论吗?),但我创建了一个自定义函数来获取它

from bs4 import BeautifulSoup
import cssutils
html='''
<html>
    <head>
        <style type="text/css">
        * {margin:0; padding:0; text-indent:0; }
        .s5 {color: #000; font-family:Verdana, sans-serif;
             font-style: normal; font-weight: normal;
             text-decoration: none; font-size: 17.5pt;
             vertical-align: 10pt;}
        </style>
    </head>

    <body>
        <p class="s1" style="padding-left: 7pt; text-indent: 0pt; text-align:left;">
        This is a sample sentence. <span class="s5"> 1</span>
        </p>
    </body>
</html>
'''
def get_property(class_name,property_name):
    for rule in sheet:
        if rule.selectorText=='.'+class_name:
            for property in rule.style:
                if property.name==property_name:
                    return property.value
soup=BeautifulSoup(html,'html.parser')
sheet=cssutils.parseString(soup.find('style').text)
vl=get_property('s5','vertical-align')
print(vl)
这并不完美,但也许您可以改进它。

要改进:

对于内联
style=“…”
标记:

import cssutils

# get the style from beautiful soup, like: style = tag['style']
style = "color: hotpink; background-color:#ff0000; visibility:hidden"

parsed_style = cssutils.parseStyle(style)
现在使用
parsed_样式
,就像使用
dict

print(parsed_style['color'])  # hotpink
print(parsed_style['background-color'])  # f00
print(parsed_style['visibility'])  # hidden



你查过了吗?我在文档中找不到任何与此相关的内容
print(parsed_style['color'])  # hotpink
print(parsed_style['background-color'])  # f00
print(parsed_style['visibility'])  # hidden