2014-06-22 68 views
1

如果我有一個XML標籤是這樣的:的Python:獲取屬性

<article n="1" translation="Year_1973_fr.xml:1"> 

我怎樣才能訪問「翻譯」 -Attribute?

這是訪問的 「N」 -attribute沒有問題,我只是做到以下幾點: s.attrib [ 「N」]

感謝您的任何意見。

+3

是否's.attrib [ 「翻譯」]'工作? –

+0

你在使用任何庫嗎?有任何代碼?你有沒有得到任何錯誤?這個問題太廣泛了,不能很好地回答。 –

回答

3

.attrib["translation"]作品:

>>> from xml.etree import ElementTree as ET 
>>> data = '<article n="1" translation="Year_1973_fr.xml:1"/>' 
>>> element = ET.fromstring(data) 
>>> element.attrib 
{'translation': 'Year_1973_fr.xml:1', 'n': '1'} 
>>> element.attrib['translation'] 
'Year_1973_fr.xml:1' 
+0

非常感謝!我有一個錯字,這就是爲什麼它沒有工作...:/ – MarkF6

1

例如使用BeautifulSoup

html_doc = """ 
<article n="1" translation="Year_1973_fr.xml:1"> 
""" 
from bs4 import BeautifulSoup 
soup = BeautifulSoup(html_doc) 
print soup.article['translation'] 
Year_1973_fr.xml:1