擅长:python、mysql、java
<p>根据您的编辑(以及原始<code>for</code>语句中的名称<code>types</code>),您似乎位于标记属性之后,而不是字符串。若要访问标记属性,请尝试按以下行执行操作:</p>
<pre><code>>>> xml = """<root><dep type="nsubj">
<governor idx="7">open</governor>
<dependent idx="5">it</dependent>
</dep>
<dep type="aux">
<governor idx="7">open</governor>
<dependent idx="6">will</dependent>
</dep>
<dep type="ccomp">
<governor idx="3">announced</governor>
<dependent idx="7">open</dependent>
</dep></root>"""
>>> soup = BeautifulSoup(xml)
>>> for dep in soup.find_all('dep'):
print dep.attrs.get('type')
nsubj
aux
ccomp
</code></pre>
<p>换言之,我认为你想要这样的东西:</p>
^{pr2}$
<p>请参阅文档<a href="http://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes" rel="nofollow">here</a>。在</p>