【发布时间】:2019-12-20 09:51:51
【问题描述】:
我有一些格式如下的 xml:
<Paragraph Type="Character">
<Text>
TED
</Text>
</Paragraph>
<Paragraph Type="Dialogue">
<Text>
I thought we had a rule against that.
</Text>
</Paragraph>
<Paragraph Type="Character">
<Text>
ANNIE
</Text>
</Paragraph>
<Paragraph Type="Dialogue">
<Text>
...oh.
我正在尝试提取数据,使其看起来像这样:
Character Dialogue
TED I thought we had a rule against that.
ANNIE ...oh.
我一直在尝试:
soup.find(Type = "Character").get_text()
soup.find(Type = "Dialogue").get_text()
一次返回一行。当我尝试做不止一个时,soup.find_all,即:
soup.find_all(Type = "Character").get_text()
我得到错误:
AttributeError: ResultSet object has no attribute 'get_text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
我知道find_all() 返回一个元素数组(感谢之前的回答:https://stackoverflow.com/a/21997788/8742237),我应该选择数组中的一个元素,但我想获取数组中的所有元素变成我上面展示的格式。
【问题讨论】:
标签: python xml beautifulsoup