bs4.Tag

class bs4.Tag(parser=None, builder=None, name=None, namespace=None, prefix=None, attrs=None, parent=None, previous=None)[source]

Represents a found HTML tag with its attributes and contents.

__init__(parser=None, builder=None, name=None, namespace=None, prefix=None, attrs=None, parent=None, previous=None)[source]

Basic constructor.

Methods

__init__([parser, builder, name, namespace, ...]) Basic constructor.
append(tag) Appends the given tag to the contents of this tag.
childGenerator()
clear([decompose]) Extract all children.
decode([indent_level, eventual_encoding, ...]) Returns a Unicode representation of this tag and its contents.
decode_contents([indent_level, ...]) Renders the contents of this tag as a Unicode string.
decompose() Recursively destroys the contents of this tree.
encode([encoding, indent_level, formatter, ...])
encode_contents([indent_level, encoding, ...]) Renders the contents of this tag as a bytestring.
extract() Destructively rips this element out of the tree.
fetchNextSiblings([name, attrs, text, limit]) Returns the siblings of this Tag that match the given criteria and appear after this Tag in the document.
fetchParents([name, attrs, limit]) Returns the parents of this Tag that match the given criteria.
fetchPrevious([name, attrs, text, limit]) Returns all items that match the given criteria and appear before this Tag in the document.
fetchPreviousSiblings([name, attrs, text, limit]) Returns the siblings of this Tag that match the given criteria and appear before this Tag in the document.
find([name, attrs, recursive, text]) Return only the first child of this Tag matching the given criteria.
findAll([name, attrs, recursive, text, limit]) Extracts a list of Tag objects that match the given criteria.
findAllNext([name, attrs, text, limit]) Returns all items that match the given criteria and appear after this Tag in the document.
findAllPrevious([name, attrs, text, limit]) Returns all items that match the given criteria and appear before this Tag in the document.
findChild([name, attrs, recursive, text]) Return only the first child of this Tag matching the given criteria.
findChildren([name, attrs, recursive, text, ...]) Extracts a list of Tag objects that match the given criteria.
findNext([name, attrs, text]) Returns the first item that matches the given criteria and appears after this Tag in the document.
findNextSibling([name, attrs, text]) Returns the closest sibling to this Tag that matches the given criteria and appears after this Tag in the document.
findNextSiblings([name, attrs, text, limit]) Returns the siblings of this Tag that match the given criteria and appear after this Tag in the document.
findParent([name, attrs]) Returns the closest parent of this Tag that matches the given criteria.
findParents([name, attrs, limit]) Returns the parents of this Tag that match the given criteria.
findPrevious([name, attrs, text]) Returns the first item that matches the given criteria and appears before this Tag in the document.
findPreviousSibling([name, attrs, text]) Returns the closest sibling to this Tag that matches the given criteria and appears before this Tag in the document.
findPreviousSiblings([name, attrs, text, limit]) Returns the siblings of this Tag that match the given criteria and appear before this Tag in the document.
find_all([name, attrs, recursive, text, limit]) Extracts a list of Tag objects that match the given criteria.
find_all_next([name, attrs, text, limit]) Returns all items that match the given criteria and appear after this Tag in the document.
find_all_previous([name, attrs, text, limit]) Returns all items that match the given criteria and appear before this Tag in the document.
find_next([name, attrs, text]) Returns the first item that matches the given criteria and appears after this Tag in the document.
find_next_sibling([name, attrs, text]) Returns the closest sibling to this Tag that matches the given criteria and appears after this Tag in the document.
find_next_siblings([name, attrs, text, limit]) Returns the siblings of this Tag that match the given criteria and appear after this Tag in the document.
find_parent([name, attrs]) Returns the closest parent of this Tag that matches the given criteria.
find_parents([name, attrs, limit]) Returns the parents of this Tag that match the given criteria.
find_previous([name, attrs, text]) Returns the first item that matches the given criteria and appears before this Tag in the document.
find_previous_sibling([name, attrs, text]) Returns the closest sibling to this Tag that matches the given criteria and appears before this Tag in the document.
find_previous_siblings([name, attrs, text, ...]) Returns the siblings of this Tag that match the given criteria and appear before this Tag in the document.
format_string(s[, formatter]) Format the given string using the given formatter.
get(key[, default]) Returns the value of the ‘key’ attribute for the tag, or the value given for ‘default’ if it doesn’t have that attribute.
getText([separator, strip, types]) Get all child strings, concatenated using the given separator.
get_text([separator, strip, types]) Get all child strings, concatenated using the given separator.
has_attr(key)
has_key(key) This was kind of misleading because has_key() (attributes) was different from __in__ (contents).
index(element) Find the index of a child by identity, not value.
insert(position, new_child)
insert_after(successor) Makes the given element the immediate successor of this one.
insert_before(predecessor) Makes the given element the immediate predecessor of this one.
nextGenerator()
nextSiblingGenerator()
parentGenerator()
prettify([encoding, formatter])
previousGenerator()
previousSiblingGenerator()
recursiveChildGenerator()
renderContents([encoding, prettyPrint, ...])
replaceWith(replace_with)
replaceWithChildren()
replace_with(replace_with)
replace_with_children()
select(selector[, _candidate_generator, limit]) Perform a CSS selection operation on the current element.
select_one(selector) Perform a CSS selection operation on the current element.
setup([parent, previous_element, ...]) Sets up the initial relations between this element and other elements.
unwrap()
wrap(wrap_inside)

Attributes

HTML_FORMATTERS
XML_FORMATTERS
attribselect_re
children
descendants
isSelfClosing Is this tag an empty-element tag? (aka a self-closing tag)
is_empty_element Is this tag an empty-element tag? (aka a self-closing tag)
next
nextSibling
next_elements
next_siblings
parents
parserClass
previous
previousSibling
previous_elements
previous_siblings
string Convenience property to get the single string within this tag.
strings Yield all strings of certain classes, possibly stripping them.
stripped_strings
tag_name_re
text Get all child strings, concatenated using the given separator.