我有一个来自HTML的汤的部分转换XML文档。一些置换和编辑的汤后,身体基本上是 -在Python BeautifulSoup如何移动标签
<Text...></Text> # This replaces <a href..> tags but automatically creates the </Text>
<p class=norm ...</p>
<p class=norm ...</p>
<Text...></Text>
<p class=norm ...</p> and so forth.
我需要“动”的<p>
标签是孩子<Text>
或不知道如何抑制</Text>
。我想 -
<Text...>
<p class=norm ...</p>
<p class=norm ...</p>
</Text>
<Text...>
<p class=norm ...</p>
</Text>
我试过使用item.insert和item.append,但我想那里必须有一个更优雅的解决方案。
for item in soup.findAll(['p','span']):
if item.name == 'span' and item.has_key('class') and item['class'] == 'section':
xBCV = short_2_long(item._getAttrMap().get('value',''))
if currentnode:
pass
currentnode = Tag(soup,'Text', attrs=[('TypeOf', 'Section'),... ])
item.replaceWith(currentnode) # works but creates end tag
elif item.name == 'p' and item.has_key('class') and item['class'] == 'norm':
childcdatanode = None
for ahref in item.findAll('a'):
if childcdatanode:
pass
newlink = filter_hrefs(str(ahref))
childcdatanode = Tag(soup, newlink)
ahref.replaceWith(childcdatanode)
感谢
只要你知道JJ, html/xml标签不会出现在你的问题中,除非你用“s”来转义它们,如果它是句子中的一小块,或者在适当的时候使它们成为代码块。这次我修好了,但我认为你应该知道未来。 – 2010-04-28 20:31:33
谢谢。我想知道这是如何工作的。我感谢您的监督和协助。 – user3591360 2010-04-30 13:00:25