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 | child |
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 |
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 |
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 |
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 |
The same as get(), but always returns a list. |
Method | has |
Does this PageElement have an attribute with the given name? |
Method | has |
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 | recursive |
Deprecated generator. |
Method | render |
Deprecated method for BS3 compatibility. |
Method | select |
Perform a CSS selection operation on the current element. |
Method | select |
Perform a CSS selection operation on the current element. |
Method | smooth |
Smooth out this element's children by consolidating consecutive strings. |
Method | string |
Replace this PageElement's contents with `string`. |
Constant | DEFAULT |
Undocumented |
Class Variable | parser |
Undocumented |
Class Variable | strings |
Undocumented |
Instance Variable | attrs |
Undocumented |
Instance Variable | can |
Undocumented |
Instance Variable | cdata |
Undocumented |
Instance Variable | contents |
Undocumented |
Instance Variable | hidden |
Undocumented |
Instance Variable | interesting |
Undocumented |
Instance Variable | known |
Undocumented |
Instance Variable | name |
Undocumented |
Instance Variable | namespace |
Undocumented |
Instance Variable | parser |
Undocumented |
Instance Variable | prefix |
Undocumented |
Instance Variable | preserve |
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 |
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 |
Yield all strings of certain classes, possibly stripping them. |
Method | _should |
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 |
Find all PageElements that match the given criteria and appear later in the document than this PageElement. |
Method | find |
Look backwards in the document from this PageElement and find all PageElements that match the given criteria. |
Method | find |
Find the first PageElement that matches the given criteria and appears later in the document than this PageElement. |
Method | find |
Find the closest sibling to this PageElement that matches the given criteria and appears later in the document. |
Method | find |
Find all siblings of this PageElement that match the given criteria and appear later in the document. |
Method | find |
Find the closest parent of this PageElement that matches the given criteria. |
Method | find |
Find all parents of this PageElement that match the given criteria. |
Method | find |
Look backwards in the document from this PageElement and find the first PageElement that matches the given criteria. |
Method | find |
Returns the closest sibling to this PageElement that matches the given criteria and appears earlier in the document. |
Method | find |
Returns all siblings to this PageElement that match the given criteria and appear earlier in the document. |
Method | format |
Format the given string using the given formatter. |
Method | formatter |
Look up or create a Formatter for the given identifier, if necessary. |
Method | get |
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 |
Makes the given element(s) the immediate successor of this one. |
Method | insert |
Makes the given element(s) the immediate predecessor of this one. |
Method | next |
Undocumented |
Method | next |
Undocumented |
Method | parent |
Undocumented |
Method | previous |
Undocumented |
Method | previous |
Undocumented |
Method | replace |
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 | next |
Undocumented |
Class Variable | previous |
Undocumented |
Class Variable | text |
Undocumented |
Instance Variable | next |
Undocumented |
Instance Variable | next |
Undocumented |
Instance Variable | parent |
Undocumented |
Instance Variable | previous |
Undocumented |
Instance Variable | previous |
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 |
All PageElements that were parsed after this one. |
Property | next |
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 |
All PageElements that were parsed before this one. |
Property | previous |
All PageElements that are siblings of this one but were parsed earlier. |
Property | stripped |
Yield all strings in this PageElement, stripping them first. |
Method | _find |
Iterates over a generator looking for things that match. |
Method | _find |
Undocumented |
Method | _last |
Finds the last element beneath this object to be parsed. |
Property | _is |
Is this element part of an XML tree or an HTML tree? |
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.
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.
Returns true iff this Tag has the same name, the same attributes, and the same contents (recursively) as `other`.
tag[key] returns the value of the 'key' attribute for the Tag, and throws an exception if it's not there.
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.
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.
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().
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.
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.
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.
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.
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.
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
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
Returns the value of the 'key' attribute for the tag, or the value given for 'default' if it doesn't have that attribute.
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.
Deprecated method. This was kind of misleading because has_key() (attributes) was different from __in__ (contents). has_key() is gone in Python 3, anyway.
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`.
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).
Deprecated method for BS3 compatibility.
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
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
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.
Iterate over all children of this PageElement in a breadth-first sequence. :yield: A sequence of PageElements.
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.
bs4.element.PageElement._all_strings
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.