【发布时间】:2018-10-12 14:40:58
【问题描述】:
我只想从 profile name="4" 中提取 stuff 标签。我在下面编写了一个代码,它提取了 profile name = "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>
【问题讨论】:
标签: python beautifulsoup