【发布时间】:2018-11-07 07:09:35
【问题描述】:
我必须转换一些 beautifulsoup 代码。 基本上我想要的只是获取正文节点的所有子节点并选择具有文本并存储它们。 这是 bs4 的代码:
def get_children(self, tag, dorecursive=False):
children = []
if not tag :
return children
for t in tag.findChildren(recursive=dorecursive):
if t.name in self.text_containers \
and len(t.text) > self.min_text_length \
and self.is_valid_tag(t):
children.append(t)
return children
这很好用 当我用 lxml lib 尝试这个时,孩子是空的:
def get_children(self, tag, dorecursive=False):
children = []
if not tag :
return children
tags = tag.getchildren()
for t in tags:
#print(t.tag)
if t.tag in self.text_containers \
and len(t.tail) > self.min_text_length \
and self.is_valid_tag(t):
children.append(t)
return children
有什么想法吗?
【问题讨论】:
标签: beautifulsoup lxml