英文:
BeautifulSoup TypeError: 'NoneType' object is not callable
问题
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 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.
英文:
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.
答案1
得分: 2
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.
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论