How to get custom css style in pyquery

You can set the 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 suggestion. [<p>] p.attr('style') # too much information. 'font-size: 16px; font-weight: bold' 

It seems weird, uncomfortable and unpyquery and unpython like it! One of the first two should return the style text, of course?

Is there a way to get one CSS style without using tools like split ()?

+7
source share
3 answers

p.css.font_size

It works?

+1
source

You can split the string and convert it to a dict.

+1
source

You can see from your comment that you already understood this, but PyQuery does not provide this function.

The css method (line 824 in pyquery.py) only sets the attributes and cannot get what is a shame. Although, given that PyQuery does not work with a list of styles, the usefulness of restoring CSS is limited anyway.

Manually sorting p.attr(style) results p.attr(style) be the best way at the moment. I would like to see a parameter for extracting css values ​​if only one argument is passed to the css method, but we will see.

+1
source

All Articles