bs4.css#

Integration code for CSS selectors using Soup Sieve (pypi: soupsieve).

Module Contents#

Classes#

CSS

A proxy object against the soupsieve library, to simplify its

Attributes#

bs4.css.soupsieve#
class bs4.css.CSS(tag, api=soupsieve)#

Bases: object

A proxy object against the soupsieve library, to simplify its CSS selector API.

Acquire this object through the .css attribute on the BeautifulSoup object, or on the Tag you want to use as the starting point for a CSS selector.

The main advantage of doing this is that the tag to be selected against doesn’t need to be explicitly specified in the function calls, since it’s already scoped to a tag.

escape(ident)#

Escape a CSS identifier.

This is a simple wrapper around soupselect.escape(). See the documentation for that function for more information.

_ns(ns, select)#

Normalize a dictionary of namespaces.

_rs(results)#

Normalize a list of results to a Resultset.

A ResultSet is more consistent with the rest of Beautiful Soup’s API, and ResultSet.__getattr__ has a helpful error message if you try to treat a list of results as a single result (a common mistake).

compile(select, namespaces=None, flags=0, **kwargs)#

Pre-compile a selector and return the compiled object.

Parameters:
  • selector – A CSS selector.

  • namespaces – A dictionary mapping namespace prefixes used in the CSS selector to namespace URIs. By default, Beautiful Soup will use the prefixes it encountered while parsing the document.

  • flags – Flags to be passed into Soup Sieve’s soupsieve.compile() method.

  • kwargs – Keyword arguments to be passed into SoupSieve’s soupsieve.compile() method.

Returns:

A precompiled selector object.

Return type:

soupsieve.SoupSieve

select_one(select, namespaces=None, flags=0, **kwargs)#

Perform a CSS selection operation on the current Tag and return the first result.

This uses the Soup Sieve library. For more information, see that library’s documentation for the soupsieve.select_one() method.

Parameters:
  • selector – A CSS selector.

  • namespaces – A dictionary mapping namespace prefixes used in the CSS selector to namespace URIs. By default, Beautiful Soup will use the prefixes it encountered while parsing the document.

  • flags – Flags to be passed into Soup Sieve’s soupsieve.select_one() method.

  • kwargs – Keyword arguments to be passed into SoupSieve’s soupsieve.select_one() method.

Returns:

A Tag, or None if the selector has no match.

Return type:

bs4.element.Tag

select(select, namespaces=None, limit=0, flags=0, **kwargs)#

Perform a CSS selection operation on the current Tag.

This uses the Soup Sieve library. For more information, see that library’s documentation for the soupsieve.select() method.

Parameters:
  • selector – A string containing a CSS selector.

  • namespaces – A dictionary mapping namespace prefixes used in the CSS selector to namespace URIs. By default, Beautiful Soup will pass in the prefixes it encountered while parsing the document.

  • limit – After finding this number of results, stop looking.

  • flags – Flags to be passed into Soup Sieve’s soupsieve.select() method.

  • kwargs – Keyword arguments to be passed into SoupSieve’s soupsieve.select() method.

Returns:

A ResultSet of Tag objects.

Return type:

bs4.element.ResultSet

iselect(select, namespaces=None, limit=0, flags=0, **kwargs)#

Perform a CSS selection operation on the current Tag.

This uses the Soup Sieve library. For more information, see that library’s documentation for the soupsieve.iselect() method. It is the same as select(), but it returns a generator instead of a list.

Parameters:
  • selector – A string containing a CSS selector.

  • namespaces – A dictionary mapping namespace prefixes used in the CSS selector to namespace URIs. By default, Beautiful Soup will pass in the prefixes it encountered while parsing the document.

  • limit – After finding this number of results, stop looking.

  • flags – Flags to be passed into Soup Sieve’s soupsieve.iselect() method.

  • kwargs – Keyword arguments to be passed into SoupSieve’s soupsieve.iselect() method.

Returns:

A generator

Return type:

types.GeneratorType

closest(select, namespaces=None, flags=0, **kwargs)#

Find the Tag closest to this one that matches the given selector.

This uses the Soup Sieve library. For more information, see that library’s documentation for the soupsieve.closest() method.

Parameters:
  • selector – A string containing a CSS selector.

  • namespaces – A dictionary mapping namespace prefixes used in the CSS selector to namespace URIs. By default, Beautiful Soup will pass in the prefixes it encountered while parsing the document.

  • flags – Flags to be passed into Soup Sieve’s soupsieve.closest() method.

  • kwargs – Keyword arguments to be passed into SoupSieve’s soupsieve.closest() method.

Returns:

A Tag, or None if there is no match.

Return type:

bs4.Tag

match(select, namespaces=None, flags=0, **kwargs)#

Check whether this Tag matches the given CSS selector.

This uses the Soup Sieve library. For more information, see that library’s documentation for the soupsieve.match() method.

Param:

a CSS selector.

Parameters:
  • namespaces – A dictionary mapping namespace prefixes used in the CSS selector to namespace URIs. By default, Beautiful Soup will pass in the prefixes it encountered while parsing the document.

  • flags – Flags to be passed into Soup Sieve’s soupsieve.match() method.

  • kwargs – Keyword arguments to be passed into SoupSieve’s soupsieve.match() method.

Returns:

True if this Tag matches the selector; False otherwise.

Return type:

bool

filter(select, namespaces=None, flags=0, **kwargs)#

Filter this Tag’s direct children based on the given CSS selector.

This uses the Soup Sieve library. It works the same way as passing this Tag into that library’s soupsieve.filter() method. More information, for more information see the documentation for soupsieve.filter().

Parameters:
  • namespaces – A dictionary mapping namespace prefixes used in the CSS selector to namespace URIs. By default, Beautiful Soup will pass in the prefixes it encountered while parsing the document.

  • flags – Flags to be passed into Soup Sieve’s soupsieve.filter() method.

  • kwargs – Keyword arguments to be passed into SoupSieve’s soupsieve.filter() method.

Returns:

A ResultSet of Tag objects.

Return type:

bs4.element.ResultSet