我有一个html文件,看起来像:
...
<p>
<strong>This is </strong>
<strong>a lin</strong>
<strong>e which I want to </strong>
<strong>join.</strong>
</p>
<p>
2.
<strong>But do not </strong>
<strong>touch this</strong>
<em>Maybe some other tags as well.</em>
bla bla blah...
</p>
...
我需要的是,如果所有标签都在&#39; p&#39;块是强大的,然后将它们组合成一行,即
<p>
<strong>This is a line which I want to join.</strong>
</p>
不触及另一个块,因为它包含其他内容。
有什么建议吗?我正在使用lxml。
更新
到目前为止,我试过了:
for p in self.tree.xpath('//body/p'):
if p.tail is None: #no text before first element
children = p.getchildren()
for child in children:
if len(children)==1 or child.tag!='strong' or child.tail is not None:
break
else:
etree.strip_tags(p,'strong')
使用这些代码,我能够剥离所需部分中的强标记,并给出:
<p>
This is a line which I want to join.
</p>
所以现在我只需要一种方法将标签放回......
答案 0 :(得分:2)
我能用bs4(BeautifulSoup)做到这一点:
from bs4 import BeautifulSoup as bs
html = """<p>
<strong>This is </strong>
<strong>a lin</strong>
<strong>e which I want to </strong>
<strong>join.</strong>
</p>
<p>
<strong>But do not </strong>
<strong>touch this</strong>
</p>"""
soup = bs(html)
s = ''
# note that I use the 0th <p> block ...[0],
# so make the appropriate change in your code
for t in soup.find_all('p')[0].text:
s = s+t.strip('\n')
s = '<p><strong>'+s+'</strong></p>'
print s # prints: <p><strong>This is a line which I want to join.</strong></p>
然后使用replace_with()
:
p_tag = soup.p
p_tag.replace_with(bs(s, 'html.parser'))
print soup
打印:
<html><body><p><strong>This is a line which I want to join.</strong></p>
<p>
<strong>But do not </strong>
<strong>touch this</strong>
</p></body></html>
答案 1 :(得分:2)
我设法解决了自己的问题。
for p in self.tree.xpath('//body/p'):
if p.tail is None: # some conditions specifically for my doc
children = p.getchildren()
if len(children)>1:
for child in children:
#if other stuffs present, break
if child.tag!='strong' or child.tail is not None:
break
else:
# If not break, we find a p block to fix
# Get rid of stuffs inside p, and put a SubElement in
etree.strip_tags(p,'strong')
tmp_text = p.text_content()
p.clear()
subtext = etree.SubElement(p, "strong")
subtext.text = tmp_text
特别感谢@Scott,他帮我解决了这个问题。虽然我不能正确地回答他的答案,但我对他的指导并不那么感激。
答案 2 :(得分:1)
或者,您可以使用更具体的xpath直接获取目标p
元素:
p_target = """
//p[strong]
[not(*[not(self::strong)])]
[not(text()[normalize-space()])]
"""
for p in self.tree.xpath(p_target):
#logic inside the loop can also be the same as your `else` block
content = p.xpath("normalize-space()")
p.clear()
strong = etree.SubElement(p, "strong")
strong.text = content
关于正在使用的xpath的简要说明:
//p[strong]
:在XML / HTML文档中的任何位置找到p
元素,具有子元素strong
... [not(*[not(self::strong)])]
:..并且没有strong
... [not(text()[normalize-space()])]
:..并且没有非空的文本节点子。normalize-space()
:从当前上下文元素获取所有文本节点,与标准化为单个空格的连续空格连接