我有下面的xml文件,我想提取所有的href,我知道怎么做,但是我想用----标记每个主‘父’标记的结束
我需要这样的输出:
xxxx yyyy ----- zzzz tttt ------ wwww qqqqq ssss uuuu oooo pppp ----- mmmm nnnnn ----xml:
<root> <parent id1='1111'> <child herf='xxx'/> <child herf ='yyyy'/> </parent> <parent id1='22222'> <child herf='zzzz'/> <child herf ='tttt'/> </parent> <parent id1='33333'> <child herf='wwww'/> <child herf ='qqqqq'/> <parent id1='4444'> <child herf='ssss'/> <child herf ='uuuu'/> </parent> <parent id1='55555'> <child herf='oooo'/> <child herf ='pppp'/> </parent> <parent id1='6666'> <child herf='mmmm'/> <child herf ='nnnnn'/> </parent>这是我的密码:
xml= soupTop.findChildren(recursive=False) for tag in xml: s =tag.findAll("child", {"href" : re.compile(r".*")}) print (s)发布于 2018-05-30 14:37:30
一个问题是xml是无效的。<root>标记从不关闭,<child id1='33333'>也不关闭。BS善于接受不正确的输入,但是处理它需要非常谨慎。
这意味着我无法想象有一种方法可以获得你在问题中要求的输出。我能做的是:
parent标记,并且在每个进程中只有直接的子进程。
P in soupTop.findAll('parent '):c in p.children: if c.name == 'child':print(c‘’herf‘,end =’')打印(‘
产出如下:child标记。
P=soup.find(“父”),而p不是零:p.findAll(‘p.findAll ')中的c(’child '):print(c‘’herf‘,end=’‘)打印(’--‘-’‘,end =’)p= p.findNextSibling('parent')
输出是:xxx yyyy - zzzz is qqqqq ssss nnnnnhttps://stackoverflow.com/questions/50603385
复制相似问题