如果您要避免
script使用BeautifulSoup提取标签的任何内容,
nonscripttags = htmlDom.findAll(lambda t: t.name != 'script', recursive=False)
会为您做到这一点,获取非脚本标记的根的直系子代(单独
htmlDom.findAll(recursive=False,text=True)获取根的直系子代的字符串)。您需要递归执行此操作;例如,作为发电机:
def nonscript(tag): return tag.name != 'script'def getStrings(root): for s in root.childGenerator(): if hasattr(s, 'name'): # then it's a tag if s.name == 'script': # skip it! continue for x in getStrings(s): yield x else: # it's a string! yield s
我正在使用
childGenerator(代替
findAll),以便可以按顺序排列所有子项并进行自己的过滤。



