Instead, use soup.find()
or soup.find_all()
:
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<thing>
<id>1234</id><distribution-code>555444333</distribution-code>
</thing>')
>>> soup.thing
<thing>
<id>1234</id><distribution-code>555444333</distribution-code>
</thing>
>>> soup.id
<id>1234</id>
>>> soup.distribution-code
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'code' is not defined
>>> soup.find('distribution-code')
<distribution-code>555444333</distribution-code>
An element name can contain any alphanumeric characters. The only punctuation mark allowed in names are the hyphen (-), under-score (_) and period (.).,element-name is the name of the element. The name its case in the start and end tags must match.,Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag.,XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these.
Following is the syntax to write an XML element −
<element-name attribute1 attribute2>
....content
</element-name>
element-name is the name of the element. The name its case in the start and end tags must match.
attribute1, attribute2 are attributes of the element separated by white spaces. An attribute defines a property of the element. It associates a name with a value, which is a string of characters. An attribute is written as −
name = "value"
An empty element (element with no content) has following syntax −
<name attribute1 attribute2... />
Last Updated : 24 May, 2022
Syntax:
<element-name attributes> Contents...</element-name>
Example:
name = "Geeks"
Here, Geeks represents the value of attribute
Empty Elements: An element in XML document which does not contains the content is known as Empty Element. The basic syntax of empty element in XML as follows:
<elements-name attributename />
05/24/2022
<CATEGORIES>
<CATEGORY name="category display name" refname="category reference name">
<DEFAULTWORKITEMTYPE name="work item type reference name" />
<WORKITEMTYPE name="work item type reference name" />
</CATEGORY>
</CATEGORIES>