GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • lxml
  • Why lxml?
  • Installing lxml
  • Benchmarks and Speed

This view

  • Original page
  • New address

GeezerVison is showing lxml.de — lxml.cssselect

Menu
Like the tool?
Help making it better!
Your donation helps!
  • lxml
    • lxml
      • Introduction
      • Support the project
      • Documentation
      • Download
      • Mailing list
      • Bug tracker
      • License
      • Old Versions
      • Project income report
      • Legal Notice for Donations
    • Why lxml?
      • Motto
      • Aims
    • Installing lxml
      • Where to get it
      • Requirements
      • Installation
      • Building lxml from dev sources
      • Using lxml with python-libxml2
      • Source builds on MS Windows
      • Source builds on MacOS-X
    • Benchmarks and Speed
      • General notes
      • How to read the timings
      • Parsing and Serialising
      • The ElementTree API
      • XPath
      • A longer example
      • lxml.objectify
    • ElementTree compatibility of lxml.etree
    • lxml FAQ - Frequently Asked Questions
      • General Questions
      • Installation
      • Contributing
      • Bugs
      • Threading
      • Parsing and Serialisation
      • XPath and Document Traversal
  • Developing with lxml
    • The lxml.etree Tutorial
      • The Element class
      • The ElementTree class
      • Parsing from strings and files
      • Namespaces
      • The E-factory
      • ElementPath
    • API reference
    • APIs specific to lxml.etree
      • lxml.etree
      • Other Element APIs
      • Trees and Documents
      • Iteration
      • Error handling on exceptions
      • Error logging
      • Serialisation
      • Incremental XML generation
      • CDATA
      • XInclude and ElementInclude
    • Parsing XML and HTML with lxml
      • Parsers
      • The target parser interface
      • The feed parser interface
      • Incremental event parsing
      • iterparse and iterwalk
      • Python unicode strings
    • Validation with lxml
      • Validation at parse time
      • DTD
      • RelaxNG
      • XMLSchema
      • Schematron
      • (Pre-ISO-Schematron)
    • XPath and XSLT with lxml
      • XPath
      • XSLT
    • lxml.objectify
      • The lxml.objectify API
      • Asserting a Schema
      • ObjectPath
      • Python data types
      • How data types are matched
      • What is different from lxml.etree?
    • lxml.html
      • Parsing HTML
      • HTML Element Methods
      • Running HTML doctests
      • Creating HTML with the E-factory
      • Working with links
      • Forms
      • HTML Diff
      • Examples
    • lxml.cssselect
      • The CSSSelector class
      • The cssselect method
      • Supported Selectors
      • Namespaces
    • BeautifulSoup Parser
      • Parsing with the soupparser
      • Entity handling
      • Using soupparser as a fallback
      • Using only the encoding detection
    • html5lib Parser
      • Differences to regular HTML parsing
      • Function Reference
  • Extending lxml
    • Document loading and URL resolving
      • XML Catalogs
      • URI Resolvers
      • Document loading in context
      • I/O access control in XSLT
    • Python extensions for XPath and XSLT
      • XPath Extension functions
      • XSLT extension elements
    • Using custom Element classes in lxml
      • Background on Element proxies
      • Element initialization
      • Setting up a class lookup scheme
      • Generating XML with custom classes
      • Implementing namespaces
    • Sax support
      • Building a tree from SAX events
      • Producing SAX events from an ElementTree or Element
      • Interfacing with pulldom/minidom
    • The public C-API of lxml.etree
      • Passing generated trees through Python
      • Writing external modules in Cython
      • Writing external modules in C
  • Developing lxml
    • How to build lxml from source
      • Cython
      • Github, git and hg
      • Building the sources
      • Running the tests and reporting errors
      • Building an egg or wheel
      • Building lxml on MacOS-X
      • Static linking on Windows
      • Building Debian packages from SVN sources
    • How to read the source of lxml
      • What is Cython?
      • Where to start?
      • lxml.etree
      • Python modules
      • lxml.objectify
      • lxml.html
    • Release Changelog
    • Credits
      • Main contributors
      • Special thanks goes to:
  • Sitemap
Like the tool?
Help making it better!
Your donation helps!

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.

Contents

  • The CSSSelector class
  • The cssselect method
  • Supported Selectors
  • Namespaces

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
<CSSSelector ... for 'div.content'>
>>> 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('''<div id="outer">
...   <div id="inner" class="content body">
...       text
...   </div></div>''')
>>> [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.


Generated on: 2026-06-16.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.