Issue
I have been trying to create a tag under a certain other tag in an XML tree:
from bs4 import NavigableString, Tag, ProcessingInstruction
skeleton = Soup(features='xml')
print(type(skeleton))
tei = skeleton.new_tag("TEI")
print(type(tei))
a = tei.new_tag("TT")
However, I receive a weird and repeating error:
TypeError: 'NoneType' object is not callable
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/1t/10n3n3bd6yn3sw0n1ct2110m0000gp/T/ipykernel_55721/3893923391.py in <module>
4 tei = skeleton.new_tag("TEI")
5 print(type(tei))
----> 6 a = tei.new_tag("TT")
7 tei['xmlns']= "http://www.tei-c.org/ns/1.0"
8 teiheader = skeleton.new_tag("teiheader")
TypeError: 'NoneType' object is not callable
This must work according to entry-level tutorials, but doesn't, and I don't understand why.
Solution
You need to append the newly created element to their respective parent, like below:
from bs4 import BeautifulSoup, NavigableString, Tag, ProcessingInstruction
skeleton = BeautifulSoup(features='xml')
print(type(skeleton))
tei = skeleton.new_tag("TEI")
skeleton.append(tei)
print(type(tei))
a = skeleton.new_tag("TT")
a.string = " This is new TT element nested under TEI"
tei.append(a)
print(skeleton)
Result in terminal:
<class 'bs4.BeautifulSoup'>
<class 'bs4.element.Tag'>
<?xml version="1.0" encoding="utf-8"?>
<TEI><TT> This is new TT element nested under TEI</TT></TEI>
Also, BeautifulSoup documentation.
Answered By - Barry the Platipus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.