如何在pyquery中获取单独的css样式

发布于 2024-10-17 18:28:57 字数 648 浏览 5 评论 0原文

您可以使用多种方法设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左秋 2024-10-24 18:28:57

从您的评论看来您已经弄清楚了这一点,但 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 the css method, but we shall see.

说不完的你爱 2024-10-24 18:28:57

p.css.font_size

这有效吗?

p.css.font_size

Does this work?

岁吢 2024-10-24 18:28:57

您可以拆分字符串并将其转换为字典。

You could split the string and convert it into a dict.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文