如何在pyquery中获取单独的css样式
您可以使用多种方法设置 css 样式:
p = PyQuery('<p></p>')
p.css('font-size','16px')
p.css(['font-size'] = '16px'
p.css = {'font-size':'16px'}
很好,但是如何获取单独 css 样式呢?
p.css('font-size') # jquery-like method doesn't work
[<p>]
p.css['font-size'] # pythonic method doesn't work!
[<p>]
p.css.font_size # BeardedO's suggestion.
[<p>]
p.attr('style') # too much information.
'font-size: 16px; font-weight: bold'
这看起来很奇怪,不方便,而且不像pyquery和unpython!前两个之一应该返回样式文本,当然吗?
有没有一种方法可以单独获得单个 css 样式,而无需借助 split() 等工具?
You can set a css style using several methods:
p = PyQuery('<p></p>')
p.css('font-size','16px')
p.css(['font-size'] = '16px'
p.css = {'font-size':'16px'}
Great, but how to get an individual css style?
p.css('font-size') # jquery-like method doesn't work
[<p>]
p.css['font-size'] # pythonic method doesn't work!
[<p>]
p.css.font_size # BeardedO's suggestion.
[<p>]
p.attr('style') # too much information.
'font-size: 16px; font-weight: bold'
This seems strange, inconvenient and unpyquery and unpython like! One of the first two ought to return the style text, surely?
Is there a way to get a single css style alone without resorting to tools like split()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您的评论看来您已经弄清楚了这一点,但 PyQuery 不提供此功能。
css
方法(pyquery.py 中的第 824 行)仅设置属性而无法检索,这很遗憾。尽管如此,鉴于 PyQuery 不使用样式表,检索 CSS 的实用性无论如何都是有限的。手动对
p.attr(style)
的结果进行排序似乎是目前最好的方法。我希望看到一种在仅将一个参数传递给css
方法时检索 css 值的选项,但我们将会看到。It appears from your comment that you have already figured this out, but PyQuery does not provide this functionality.
The
css
method (line 824 in pyquery.py) only sets attributes and cannot retrieve, which is a shame. Although, given that PyQuery is not working with a stylesheet, the utility of retrieving CSS is limited anyway.Manually sorting through the results of
p.attr(style)
seems to be the best way to go at the moment. I would like to see an option for retrieving css values if only one argument is passed to thecss
method, but we shall see.p.css.font_size
这有效吗?
p.css.font_size
Does this work?
您可以拆分字符串并将其转换为字典。
You could split the string and convert it into a dict.