【问题标题】:Beautifulsoup css data extractionBeautifulsoup css 数据提取
【发布时间】:2014-08-05 18:45:15
【问题描述】:

我正在尝试从 html 文档中提取 css 数据。 数据点是用户在图像上生成的可变数量的圆 x-y 坐标,并导出到 html 中,如下所示:

#shapes a#rage_circle1{
    top: 248px;
    left: 231px;
    width: 18px;
    height: 18px;
    border: 1px solid #000000;
    background-image: none;
}

我想要输出顶部和左侧的像素数。

更新:

这是我目前所做的

from bs4 import BeautifulSoup
import re
soup = BeautifulSoup (open ('index.html'))
x= soup.findAll(text=re.compile('left'))
print (x)

生成的输出是上面大括号之间的所有数据,而不是仅选择“左”字符串。我不确定为什么我的脚本没有在大括号之间选择特定数据。有什么建议吗?

【问题讨论】:

标签: python css beautifulsoup


【解决方案1】:

我认为cssutils 是解决您问题的正确选择。下面的 sn -p 将简单地输出所有 topleft 属性的值。

import cssutils
css = cssutils.parseFile('index.html')
for rule in css.cssRules:
    print(rule.style.top)
    print(rule.style.left)

【讨论】:

  • 谢谢你,这个正则表达式对于我打算做的任务来说太复杂了。抛出了一些错误,但至少这是一个开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 2021-05-30
相关资源
最近更新 更多