BeautifulSoup 类型错误:’NoneType’ 对象不可调用

huangapple go评论61阅读模式
英文:

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=&#39;xml&#39;)
print(type(skeleton))
tei = skeleton.new_tag(&quot;TEI&quot;)
print(type(tei))
a = tei.new_tag(&quot;TT&quot;)

However, I receive a weird and repeating error:

TypeError: &#39;NoneType&#39; object is not callable
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/1t/10n3n3bd6yn3sw0n1ct2110m0000gp/T/ipykernel_55721/3893923391.py in &lt;module&gt;
      4 tei = skeleton.new_tag(&quot;TEI&quot;)
      5 print(type(tei))
----&gt; 6 a = tei.new_tag(&quot;TT&quot;)
      7 tei[&#39;xmlns&#39;]= &quot;http://www.tei-c.org/ns/1.0&quot;
      8 teiheader = skeleton.new_tag(&quot;teiheader&quot;)

TypeError: &#39;NoneType&#39; 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=&#39;xml&#39;)
print(type(skeleton))
tei = skeleton.new_tag(&quot;TEI&quot;)
skeleton.append(tei)
print(type(tei))
a = skeleton.new_tag(&quot;TT&quot;)
a.string = &quot; This is new TT element nested under TEI&quot;
tei.append(a)
print(skeleton)

Result in terminal:

&lt;class &#39;bs4.BeautifulSoup&#39;&gt;
&lt;class &#39;bs4.element.Tag&#39;&gt;
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;TEI&gt;&lt;TT&gt; This is new TT element nested under TEI&lt;/TT&gt;&lt;/TEI&gt;

Also, BeautifulSoup documentation.

huangapple
  • 本文由 发表于 2023年6月1日 00:13:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定