使用python解析XML时出现属性错误
我想从youtube视频的字幕转录本中提取文本。我使用video.google.com获取XML文件。现在我想从xml文件中提取文本。我尝试了以下操作,但得到了一个使用python解析XML时出现属性错误,python,xml,Python,Xml,我想从youtube视频的字幕转录本中提取文本。我使用video.google.com获取XML文件。现在我想从xml文件中提取文本。我尝试了以下操作,但得到了一个AttributeError:“NoneType”对象没有属性“text”错误。我只添加了xml文件的一个示例,因为它可能太长 from xml.etree import cElementTree as ET xmlstring = """<timedtext format="3"> <style type="te
AttributeError:“NoneType”对象没有属性“text”
错误。我只添加了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)
从xml.etree导入cElementTree作为ET
xmlstring=”“”
在里面
这个
最后的
视频
我们
起动
说话
关于
马尔可夫
模型
作为
A.
方式
到
模型
"""
单词=[]
root=ET.fromstring(xmlstring)
对于列表中的页面(根):
words.append(page.find('s').text)
text=''.join(单词)
视频的文本在
标签中,但我无法提取它们。知道怎么办吗?提前感谢s标签位于p标签内,p标签位于body标签内。您可以稍微更改代码
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)
s标签位于p标签内,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)
您可以直接循环
s标记
root = ET.fromstring(xmlstring)
words = [s.text for s in root.findall(".//s")]
text = ' '.join(words)
您可以直接循环
s标记
root = ET.fromstring(xmlstring)
words = [s.text for s in root.findall(".//s")]
text = ' '.join(words)
非常感谢Mitiku。非常感谢Mitiku。嗯,这更干净了。谢谢,这更干净了。谢谢