class documentation

Represents an HTML or XML tag that is part of a parse tree, along with its attributes and contents. When Beautiful Soup parses the markup <b>penguin</b>, it will create a Tag object representing the <b> tag.

Method __bool__ A tag is non-None even if it has no contents.
Method __call__ Calling a Tag like a function is the same as calling its find_all() method. Eg. tag('a') returns a list of all the A tags found within this tag.
Method __contains__ Undocumented
Method __copy__ A copy of a Tag is a new Tag, unconnected to the parse tree. Its contents are a copy of the old Tag's contents.
Method __delitem__ Deleting tag[key] deletes all 'key' attributes for the tag.
Method __eq__ Returns true iff this Tag has the same name, the same attributes, and the same contents (recursively) as `other`.
Method __getattr__ Calling tag.subtag is the same as calling tag.find(name="subtag")
Method __getitem__ tag[key] returns the value of the 'key' attribute for the Tag, and throws an exception if it's not there.
Method __hash__ Undocumented
Method __init__ Basic constructor.
Method __iter__ Iterating over a Tag iterates over its contents.
Method __len__ The length of a Tag is the length of its list of contents.
Method __ne__ Returns true iff this Tag is not identical to `other`, as defined in __eq__.
Method __repr__ Renders this PageElement as a string.
Method __setitem__ Setting tag[key] sets the value of the 'key' attribute for the tag.
Method __unicode__ Renders this PageElement as a Unicode string.
Method childGenerator Deprecated generator.
Method clear Wipe out all children of this PageElement by calling extract() on them.
Method decode Render a Unicode representation of this PageElement and its contents.
Method decode_contents Renders the contents of this tag as a Unicode string.
Method decompose Recursively destroys this PageElement and its children.
Method encode Render a bytestring representation of this PageElement and its contents.
Method encode_contents Renders the contents of this PageElement as a bytestring.
Method find Look in the children of this PageElement and find the first PageElement that matches the given criteria.
Method find_all Look in the children of this PageElement and find all PageElements that match the given criteria.
Method get Returns the value of the 'key' attribute for the tag, or the value given for 'default' if it doesn't have that attribute.
Method get_attribute_list The same as get(), but always returns a list.
Method has_attr Does this PageElement have an attribute with the given name?
Method has_key Deprecated method. This was kind of misleading because has_key() (attributes) was different from __in__ (contents).
Method index Find the index of a child by identity, not value.
Method prettify Pretty-print this PageElement as a string.
Method recursiveChildGenerator Deprecated generator.
Method renderContents Deprecated method for BS3 compatibility.
Method select Perform a CSS selection operation on the current element.
Method select_one Perform a CSS selection operation on the current element.
Method smooth Smooth out this element's children by consolidating consecutive strings.
Method string.setter Replace this PageElement's contents with `string`.
Constant DEFAULT_INTERESTING_STRING_TYPES Undocumented
Class Variable parserClass Undocumented
Class Variable strings Undocumented
Instance Variable attrs Undocumented
Instance Variable can_be_empty_element Undocumented
Instance Variable cdata_list_attributes Undocumented
Instance Variable contents Undocumented
Instance Variable hidden Undocumented
Instance Variable interesting_string_types Undocumented
Instance Variable known_xml Undocumented
Instance Variable name Undocumented
Instance Variable namespace Undocumented
Instance Variable parser_class Undocumented
Instance Variable prefix Undocumented
Instance Variable preserve_whitespace_tags Undocumented
Instance Variable sourceline Undocumented
Instance Variable sourcepos Undocumented
Property children Iterate over all direct children of this PageElement.
Property descendants Iterate over all children of this PageElement in a breadth-first sequence.
Property is_empty_element Is this tag an empty-element tag? (aka a self-closing tag)
Property string Convenience property to get the single string within this PageElement.
Method _all_strings Yield all strings of certain classes, possibly stripping them.
Method _should_pretty_print Should this tag be pretty-printed?
Instance Variable _namespaces Undocumented

Inherited from PageElement:

Method append Appends the given PageElement to the contents of this one.
Method extend Appends the given PageElements to this one's contents.
Method extract Destructively rips this element out of the tree.
Method find_all_next Find all PageElements that match the given criteria and appear later in the document than this PageElement.
Method find_all_previous Look backwards in the document from this PageElement and find all PageElements that match the given criteria.
Method find_next Find the first PageElement that matches the given criteria and appears later in the document than this PageElement.
Method find_next_sibling Find the closest sibling to this PageElement that matches the given criteria and appears later in the document.
Method find_next_siblings Find all siblings of this PageElement that match the given criteria and appear later in the document.
Method find_parent Find the closest parent of this PageElement that matches the given criteria.
Method find_parents Find all parents of this PageElement that match the given criteria.
Method find_previous Look backwards in the document from this PageElement and find the first PageElement that matches the given criteria.
Method find_previous_sibling Returns the closest sibling to this PageElement that matches the given criteria and appears earlier in the document.
Method find_previous_siblings Returns all siblings to this PageElement that match the given criteria and appear earlier in the document.
Method format_string Format the given string using the given formatter.
Method formatter_for_name Look up or create a Formatter for the given identifier, if necessary.
Method get_text Get all child strings of this PageElement, concatenated using the given separator.
Method insert Insert a new PageElement in the list of this PageElement's children.
Method insert_after Makes the given element(s) the immediate successor of this one.
Method insert_before Makes the given element(s) the immediate predecessor of this one.
Method nextGenerator Undocumented
Method nextSiblingGenerator Undocumented
Method parentGenerator Undocumented
Method previousGenerator Undocumented
Method previousSiblingGenerator Undocumented
Method replace_with Replace this PageElement with one or more PageElements, keeping the rest of the tree the same.
Method setup Sets up the initial relations between this element and other elements.
Method unwrap Replace this PageElement with its contents.
Method wrap Wrap this PageElement inside another one.
Class Variable default Undocumented
Class Variable nextSibling Undocumented
Class Variable previousSibling Undocumented
Class Variable text Undocumented
Instance Variable next_element Undocumented
Instance Variable next_sibling Undocumented
Instance Variable parent Undocumented
Instance Variable previous_element Undocumented
Instance Variable previous_sibling Undocumented
Property decomposed Check whether a PageElement has been decomposed.
Property next The PageElement, if any, that was parsed just after this one.
Property next_elements All PageElements that were parsed after this one.
Property next_siblings All PageElements that are siblings of this one but were parsed later.
Property parents All PageElements that are parents of this PageElement.
Property previous The PageElement, if any, that was parsed just before this one.
Property previous_elements All PageElements that were parsed before this one.
Property previous_siblings All PageElements that are siblings of this one but were parsed earlier.
Property stripped_strings Yield all strings in this PageElement, stripping them first.
Method _find_all Iterates over a generator looking for things that match.
Method _find_one Undocumented
Method _last_descendant Finds the last element beneath this object to be parsed.
Property _is_xml Is this element part of an XML tree or an HTML tree?
def __bool__(self): (source)

A tag is non-None even if it has no contents.

def __call__(self, *args, **kwargs): (source)

Calling a Tag like a function is the same as calling its find_all() method. Eg. tag('a') returns a list of all the A tags found within this tag.

def __contains__(self, x): (source)

Undocumented

def __copy__(self): (source)
overridden in bs4.BeautifulSoup

A copy of a Tag is a new Tag, unconnected to the parse tree. Its contents are a copy of the old Tag's contents.

def __delitem__(self, key): (source)

Deleting tag[key] deletes all 'key' attributes for the tag.

def __eq__(self, other): (source)

Returns true iff this Tag has the same name, the same attributes, and the same contents (recursively) as `other`.

def __getattr__(self, tag): (source)

Calling tag.subtag is the same as calling tag.find(name="subtag")

def __getitem__(self, key): (source)

tag[key] returns the value of the 'key' attribute for the Tag, and throws an exception if it's not there.

def __hash__(self): (source)

Undocumented

def __init__(self, parser=None, builder=None, name=None, namespace=None, prefix=None, attrs=None, parent=None, previous=None, is_xml=None, sourceline=None, sourcepos=None, can_be_empty_element=None, cdata_list_attributes=None, preserve_whitespace_tags=None, interesting_string_types=None, namespaces=None): (source)
overridden in bs4.BeautifulSoup

Basic constructor. :param parser: A BeautifulSoup object. :param builder: A TreeBuilder. :param name: The name of the tag. :param namespace: The URI of this Tag's XML namespace, if any. :param prefix: The prefix for this Tag's XML namespace, if any. :param attrs: A dictionary of this Tag's attribute values. :param parent: The PageElement to use as this Tag's parent. :param previous: The PageElement that was parsed immediately before this tag. :param is_xml: If True, this is an XML tag. Otherwise, this is an HTML tag. :param sourceline: The line number where this tag was found in its source document. :param sourcepos: The character position within `sourceline` where this tag was found. :param can_be_empty_element: If True, this tag should be represented as <tag/>. If False, this tag should be represented as <tag></tag>. :param cdata_list_attributes: A list of attributes whose values should be treated as CDATA if they ever show up on this tag. :param preserve_whitespace_tags: A list of tag names whose contents should have their whitespace preserved. :param interesting_string_types: This is a NavigableString subclass or a tuple of them. When iterating over this Tag's strings in methods like Tag.strings or Tag.get_text, these are the types of strings that are interesting enough to be considered. The default is to consider NavigableString and CData the only interesting string subtypes. :param namespaces: A dictionary mapping currently active namespace prefixes to URIs. This can be used later to construct CSS selectors.

def __iter__(self): (source)

Iterating over a Tag iterates over its contents.

def __len__(self): (source)

The length of a Tag is the length of its list of contents.

def __ne__(self, other): (source)

Returns true iff this Tag is not identical to `other`, as defined in __eq__.

def __repr__(self, encoding='unicode-escape'): (source)

Renders this PageElement as a string. :param encoding: The encoding to use (Python 2 only). TODO: This is now ignored and a warning should be issued if a value is provided. :return: A (Unicode) string.

def __setitem__(self, key, value): (source)

Setting tag[key] sets the value of the 'key' attribute for the tag.

def __unicode__(self): (source)

Renders this PageElement as a Unicode string.

def childGenerator(self): (source)

Deprecated generator.

def clear(self, decompose=False): (source)

Wipe out all children of this PageElement by calling extract() on them. :param decompose: If this is True, decompose() (a more destructive method) will be called instead of extract().

def decode(self, indent_level=None, eventual_encoding=DEFAULT_OUTPUT_ENCODING, formatter='minimal'): (source)
overridden in bs4.BeautifulSoup

Render a Unicode representation of this PageElement and its contents. :param indent_level: Each line of the rendering will be indented this many spaces. Used internally in recursive calls while pretty-printing. :param eventual_encoding: The tag is destined to be encoded into this encoding. This method is _not_ responsible for performing that encoding. This information is passed in so that it can be substituted in if the document contains a <META> tag that mentions the document's encoding. :param formatter: A Formatter object, or a string naming one of the standard formatters.

def decode_contents(self, indent_level=None, eventual_encoding=DEFAULT_OUTPUT_ENCODING, formatter='minimal'): (source)

Renders the contents of this tag as a Unicode string. :param indent_level: Each line of the rendering will be indented this many levels. (The formatter decides what a 'level' means in terms of spaces or other characters output.) Used internally in recursive calls while pretty-printing. :param eventual_encoding: The tag is destined to be encoded into this encoding. decode_contents() is _not_ responsible for performing that encoding. This information is passed in so that it can be substituted in if the document contains a <META> tag that mentions the document's encoding. :param formatter: A Formatter object, or a string naming one of the standard Formatters.

def decompose(self): (source)

Recursively destroys this PageElement and its children. This element will be removed from the tree and wiped out; so will everything beneath it. The behavior of a decomposed PageElement is undefined and you should never use one for anything, but if you need to _check_ whether an element has been decomposed, you can use the `decomposed` property.

def encode(self, encoding=DEFAULT_OUTPUT_ENCODING, indent_level=None, formatter='minimal', errors='xmlcharrefreplace'): (source)

Render a bytestring representation of this PageElement and its contents. :param encoding: The destination encoding. :param indent_level: Each line of the rendering will be indented this many levels. (The formatter decides what a 'level' means in terms of spaces or other characters output.) Used internally in recursive calls while pretty-printing. :param formatter: A Formatter object, or a string naming one of the standard formatters. :param errors: An error handling strategy such as 'xmlcharrefreplace'. This value is passed along into encode() and its value should be one of the constants defined by Python. :return: A bytestring.

def encode_contents(self, indent_level=None, encoding=DEFAULT_OUTPUT_ENCODING, formatter='minimal'): (source)

Renders the contents of this PageElement as a bytestring. :param indent_level: Each line of the rendering will be indented this many levels. (The formatter decides what a 'level' means in terms of spaces or other characters output.) Used internally in recursive calls while pretty-printing. :param eventual_encoding: The bytestring will be in this encoding. :param formatter: A Formatter object, or a string naming one of the standard Formatters. :return: A bytestring.

def find(self, name=None, attrs={}, recursive=True, string=None, **kwargs): (source)

Look in the children of this PageElement and find the first PageElement that matches the given criteria. All find_* methods take a common set of arguments. See the online documentation for detailed explanations. :param name: A filter on tag name. :param attrs: A dictionary of filters on attribute values. :param recursive: If this is True, find() will perform a recursive search of this PageElement's children. Otherwise, only the direct children will be considered. :param limit: Stop looking after finding this many results. :kwargs: A dictionary of filters on attribute values. :return: A PageElement. :rtype: bs4.element.Tag | bs4.element.NavigableString

def find_all(self, name=None, attrs={}, recursive=True, string=None, limit=None, **kwargs): (source)

Look in the children of this PageElement and find all PageElements that match the given criteria. All find_* methods take a common set of arguments. See the online documentation for detailed explanations. :param name: A filter on tag name. :param attrs: A dictionary of filters on attribute values. :param recursive: If this is True, find_all() will perform a recursive search of this PageElement's children. Otherwise, only the direct children will be considered. :param limit: Stop looking after finding this many results. :kwargs: A dictionary of filters on attribute values. :return: A ResultSet of PageElements. :rtype: bs4.element.ResultSet

def get(self, key, default=None): (source)

Returns the value of the 'key' attribute for the tag, or the value given for 'default' if it doesn't have that attribute.

def get_attribute_list(self, key, default=None): (source)

The same as get(), but always returns a list. :param key: The attribute to look for. :param default: Use this value if the attribute is not present on this PageElement. :return: A list of values, probably containing only a single value.

def has_attr(self, key): (source)

Does this PageElement have an attribute with the given name?

def has_key(self, key): (source)

Deprecated method. This was kind of misleading because has_key() (attributes) was different from __in__ (contents). has_key() is gone in Python 3, anyway.

def index(self, element): (source)

Find the index of a child by identity, not value. Avoids issues with tag.contents.index(element) getting the index of equal elements. :param element: Look for this PageElement in `self.contents`.

def prettify(self, encoding=None, formatter='minimal'): (source)

Pretty-print this PageElement as a string. :param encoding: The eventual encoding of the string. If this is None, a Unicode string will be returned. :param formatter: A Formatter object, or a string naming one of the standard formatters. :return: A Unicode string (if encoding==None) or a bytestring (otherwise).

def recursiveChildGenerator(self): (source)

Deprecated generator.

def renderContents(self, encoding=DEFAULT_OUTPUT_ENCODING, prettyPrint=False, indentLevel=0): (source)

Deprecated method for BS3 compatibility.

def select(self, selector, namespaces=None, limit=None, **kwargs): (source)

Perform a CSS selection operation on the current element. This uses the SoupSieve library. :param selector: A string containing a CSS selector. :param 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. :param limit: After finding this number of results, stop looking. :param kwargs: Keyword arguments to be passed into SoupSieve's soupsieve.select() method. :return: A ResultSet of Tags. :rtype: bs4.element.ResultSet

def select_one(self, selector, namespaces=None, **kwargs): (source)

Perform a CSS selection operation on the current element. :param selector: A CSS selector. :param 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. :param kwargs: Keyword arguments to be passed into SoupSieve's soupsieve.select() method. :return: A Tag. :rtype: bs4.element.Tag

def smooth(self): (source)

Smooth out this element's children by consolidating consecutive strings. This makes pretty-printed output look more natural following a lot of operations that modified the tree.

@string.setter
def string(self, string): (source)

Replace this PageElement's contents with `string`.

DEFAULT_INTERESTING_STRING_TYPES = (source)

Undocumented

Value
(NavigableString, CData)
parserClass = (source)

Undocumented

Undocumented

Undocumented

can_be_empty_element = (source)

Undocumented

cdata_list_attributes = (source)

Undocumented

contents: list = (source)

Undocumented

overridden in bs4.BeautifulSoup

Undocumented

interesting_string_types = (source)

Undocumented

known_xml = (source)
overridden in bs4.BeautifulSoup

Undocumented

Undocumented

namespace = (source)

Undocumented

parser_class = (source)

Undocumented

Undocumented

preserve_whitespace_tags = (source)

Undocumented

sourceline = (source)

Undocumented

sourcepos = (source)

Undocumented

Iterate over all direct children of this PageElement. :yield: A sequence of PageElements.

@property
descendants = (source)

Iterate over all children of this PageElement in a breadth-first sequence. :yield: A sequence of PageElements.

@property
is_empty_element = (source)

Is this tag an empty-element tag? (aka a self-closing tag) A tag that has contents is never an empty-element tag. A tag that has no contents may or may not be an empty-element tag. It depends on the builder used to create the tag. If the builder has a designated list of empty-element tags, then only a tag whose name shows up in that list is considered an empty-element tag. If the builder has no designated list of empty-element tags, then any tag with no contents is an empty-element tag.

Convenience property to get the single string within this PageElement. TODO It might make sense to have NavigableString.string return itself. :return: If this element has a single string child, return value is that string. If this element has one child tag, return value is the 'string' attribute of the child tag, recursively. If this element is itself a string, has no children, or has more than one child, return value is None.

def _all_strings(self, strip=False, types=PageElement.default): (source)

Yield all strings of certain classes, possibly stripping them. :param strip: If True, all strings will be stripped before being yielded. :param types: A tuple of NavigableString subclasses. Any strings of a subclass not found in this list will be ignored. By default, the subclasses considered are the ones found in self.interesting_string_types. If that's not specified, only NavigableString and CData objects will be considered. That means no comments, processing instructions, etc. :yield: A sequence of strings.

def _should_pretty_print(self, indent_level): (source)

Should this tag be pretty-printed? Most of them should, but some (such as <pre> in HTML documents) should not.

_namespaces = (source)
overridden in bs4.BeautifulSoup

Undocumented