html select css,lxml.cssselect

lxml.cssselect

lxml supports a number of interesting languages for tree traversal and element

selection. The most important is obviously XPath, but there is also

ObjectPath in the lxml.objectify module. The newest child of this family

is CSS selection, which is made available in form of the lxml.cssselect

module.

Although it started its life in lxml, cssselect is now an independent project.

It translates CSS selectors to XPath 1.0 expressions that can be used with

lxml's XPath engine. lxml.cssselect adds a few convenience shortcuts into

that package.

To install cssselect, run

pip install cssselect

lxml will then import and use it automatically.

The CSSSelector class

The most important class in the lxml.cssselect module is CSSSelector. It

provides the same interface as the XPath class, but accepts a CSS selector

expression as input:

>>>from lxml.cssselect import CSSSelector

>>>sel = CSSSelector('div.content')

>>>sel #doctest: +ELLIPSIS

>>>sel.css

'div.content'

The selector actually compiles to XPath, and you can see the

expression by inspecting the object:

>>>sel.path

"descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' content ')]"

To use the selector, simply call it with a document or element

object:

>>>from lxml.etree import fromstring

>>>h = fromstring('''

...

...text

...

''')

>>>[e.get('id') for e in sel(h)]

['inner']

Using CSSSelector is equivalent to translating with cssselect

and using the XPath class:

>>>from cssselect import GenericTranslator

>>>from lxml.etree import XPath

>>>sel = XPath(GenericTranslator().css_to_xpath('div.content'))

CSSSelector takes a translator parameter to let you choose which

translator to use. It can be 'xml' (the default), 'xhtml', 'html'

or a Translator object.

The cssselect method

lxml Element objects have a cssselect convenience method.

>>>h.cssselect('div.content') == sel(h)

True

Note however that pre-compiling the expression with the CSSSelector or

XPath class can provide a substantial speedup.

The method also accepts a translator parameter. On HtmlElement

objects, the default is changed to 'html'.

Supported Selectors

Most Level 3 selectors are supported. The details are in the

cssselect documentation.

Namespaces

In CSS you can use namespace-prefix|element, similar to

namespace-prefix:element in an XPath expression. In fact, it maps

one-to-one, and the same rules are used to map namespace prefixes to

namespace URIs: the CSSSelector class accepts a dictionary as its

namespaces argument.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值