使用python解析XML时的属性错误

问题描述:

我想从YouTube视频的字幕脚本中提取文本。我使用video.google.com获取了XML文件。现在我想从xml文件中提取文本。我尝试了以下,但我得到一个AttributeError: 'NoneType' object has no attribute 'text'错误。我只添加了一个xml文件的样本,因为它可能会变得太长。使用python解析XML时的属性错误

from xml.etree import cElementTree as ET 
xmlstring = """<timedtext format="3"> 
<style type="text/css" id="night-mode-pro-style"/> 
<link type="text/css" rel="stylesheet" id="night-mode-pro-link"/> 
<head> 
<pen id="1" fc="#E5E5E5"/> 
<pen id="2" fc="#CCCCCC"/> 
<ws id="0"/> 
<ws id="1" mh="2" ju="0" sd="3"/> 
<wp id="0"/> 
<wp id="1" ap="6" ah="20" av="100" rc="2" cc="40"/> 
</head> 
<body> 
<w t="0" id="1" wp="1" ws="1"/> 
<p t="30" d="5010" w="1"> 
<s ac="252">in</s> 
<s t="569" ac="252">the</s> 
<s t="1080" ac="252">last</s> 
<s t="1260" ac="227">video</s> 
<s p="2" t="1500" ac="187">we</s> 
<s p="2" t="1860" ac="160">started</s> 
<s p="2" t="2190" ac="234">talking</s> 
</p> 
<p t="2570" d="2470" w="1" a="1"></p> 
<p t="2580" d="5100" w="1"> 
<s ac="252">about</s> 
<s t="59" ac="227">Markov</s> 
<s t="660" ac="252">models</s> 
<s p="1" t="1200" ac="217">as</s> 
<s t="1379" ac="252">a</s> 
<s t="1440" ac="252">way</s> 
<s t="1949" ac="252">to</s> 
<s t="2009" ac="252">model</s> 
</p> 
</body> 
</timedtext>""" 

words = [] 
root = ET.fromstring(xmlstring) 
for page in list(root): 
    words.append(page.find('s').text) 

text = ' '.join(words) 

视频的文本是在<s>标签,但我不能提取它们。任何想法该怎么办?在此先感谢

在p标签内找到s标签,在身体标签内找到p标签。您可以稍微更改代码。

words = [] 
root = ET.fromstring(xmlstring) 
body = root.find("body") 

for page in body.findall("p"): 
    for s in page.findall("s"): 
     words.append(s.text) 

text = ' '.join(words) 
+0

非常感谢Mitiku。 –

您可以循环s tag直接

root = ET.fromstring(xmlstring) 
words = [s.text for s in root.findall(".//s")] 
text = ' '.join(words) 
+0

嗯,这很干净。谢谢 –