【发布时间】:2011-02-13 12:20:02
【问题描述】:
我有一个来自 HTML 的部分转换的 XML 文档。在汤中进行了一些替换和编辑后,主体本质上是-
<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 转义它们(如果它是句子中的一小部分),或者如果它们是代码块合适的。这次我修好了,但我认为你应该知道未来。
-
谢谢。我想知道这是如何工作的。感谢您的监督和帮助。
标签: python xml regex beautifulsoup children