在使用beautifulsoup4(根据需要安装了lxml)以xml(word / document.xml)的形式解析.docx文件内容时,我遇到了一个问题。 这部分来自xml: 成为这个: 即使我只是解析文件并保存它,也没有任何修改。 像这样: 或者从python ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我只想从配置文件名称=“ 4”中提取填充标签。 我在下面编写了一个代码,该代码提取了概要文件名称=“ 4”下的所有内容,但是有没有一种方法可以收集所有的东西标签,或者我必须使用split来将东西放入东西标签中。 我拥有的xml文件更长,因此使用split是可行的,但解析数据将花费更长的时间。
这是python代码
import bs4 as bs
# opens xml file and allows bs4 to parse xml file
xml_file = open('file.xml')
soup = bs.BeautifulSoup(xml_file, 'html.parser')
#extracts and prints all tags under profile name = "4"
stuff = soup.find_all('profile', {'name':"4"})
print stuff
这是xml文件,其名为file.xml。 我想从配置文件名称=“ 4”中提取填充标签
<profiles>
<profile name="1">
<content>apple</content>
</profile>
<profile name="2">
<content>peas</content>
</profile>
<profile name="3">
<stuff>bear</stuff>
</profile>
<profile name="4">
<content>cat</content>
<data>
<stuff>fish</stuff>
</data>
<stuff>hat</stuff>
</profile>
</profiles>
对内部标签执行相同的操作
print([i.find_all('stuff') for i in stuff])
如果您只需要标签内的数据
for i in stuff:
for x in i.find_all('stuff'):
print(x.next)
输出:
fish
hat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.