[英]python3 parsing XML
我有以下xml,其中包含各種電子郵件服務提供商的電子郵件配置,並且我試圖將這些信息解析為字典; 主機名,is_ssl,端口,協議..etc
<domains>
<domain>
<name>zoznam.sk</name>
<description>Zoznam Slovakia</description>
<service>
<hostname>imap.zoznam.sk</hostname>
<port>143</port>
<protocol>IMAP</protocol>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
<service>
<hostname>smtp.zoznam.sk</hostname>
<port>587</port>
<protocol>SMTP</protocol>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
</domain>
<domain>
<name>123mail.org</name>
<description>123mail.org</description>
<service>
<hostname>imap.fastmail.com</hostname>
<port>993</port>
<protocol>IMAP</protocol>
<ssl/>
<requires/>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
<service>
<hostname>smtp.fastmail.com</hostname>
<port>587</port>
<protocol>SMTP</protocol>
<ssl/>
<requires/>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
</domain>
<domain>
<name>Netvigator.com</name>
<description>netvigator.com</description>
<service>
<hostname>corpmail1.netvigator.com</hostname>
<port>995</port>
<protocol>POP</protocol>
<ssl/>
<authentication>NONE</authentication>
<usernameIncludesDomain/>
</service>
<service>
<hostname>corpmail1.netvigator.com</hostname>
<port>587</port>
<protocol>SMTP</protocol>
<ssl/>
<authentication>NONE</authentication>
<usernameIncludesDomain/>
</service>
</domain>
</domains>
我試圖解析名稱以進行測試,但無法成功,我需要使用python。
import xml.etree.ElementTree as ET
configs_file = 'isp_list.xml'
def parseXML(xmlfile):
# create element tree object
tree = ET.parse(xmlfile)
# get root element
root = tree.getroot()
# create empty list for configs items
configs = []
# iterate items
for item in root.findall('domains/domain'):
value = item.get('name')
# test
print(value)
# append news dictionary to items list
configs.append(item)
# return items list
return configs
我感謝您的幫助。 謝謝。
您仍然可以使用bs4生成字典。
對於if else行,您可以使用更緊湊的語法,例如
'ssl' : getattr(item.find('ssl'), 'text', 'N/A')
腳本:
from bs4 import BeautifulSoup as bs
xml = '''
<domains>
<domain>
<name>zoznam.sk</name>
<description>Zoznam Slovakia</description>
<service>
<hostname>imap.zoznam.sk</hostname>
<port>143</port>
<protocol>IMAP</protocol>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
<service>
<hostname>smtp.zoznam.sk</hostname>
<port>587</port>
<protocol>SMTP</protocol>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
</domain>
<domain>
<name>123mail.org</name>
<description>123mail.org</description>
<service>
<hostname>imap.fastmail.com</hostname>
<port>993</port>
<protocol>IMAP</protocol>
<ssl/>
<requires/>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
<service>
<hostname>smtp.fastmail.com</hostname>
<port>587</port>
<protocol>SMTP</protocol>
<ssl/>
<requires/>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
</domain>
<domain>
<name>Netvigator.com</name>
<description>netvigator.com</description>
<service>
<hostname>corpmail1.netvigator.com</hostname>
<port>995</port>
<protocol>POP</protocol>
<ssl/>
<authentication>NONE</authentication>
<usernameIncludesDomain/>
</service>
<service>
<hostname>corpmail1.netvigator.com</hostname>
<port>587</port>
<protocol>SMTP</protocol>
<ssl/>
<authentication>NONE</authentication>
<usernameIncludesDomain/>
</service>
</domain>
</domains>
'''
data = {}
soup = bs(xml, 'lxml')
for domain in soup.select('domain'):
name = domain.select_one('name').text
data[name] = {
'name' : name,
'desc' : domain.select_one('description').text,
'services' : {}
}
i = 1
for item in domain.select('service'):
service = {
'hostname' : item.select_one('hostname').text if item.select_one('hostname') else 'N/A',
'port' : item.select_one('port').text if item.select_one('port') else 'N/A',
'protocol' : item.select_one('protocol').text if item.select_one('protocol').text else 'N/A',
'ssl' : item.select_one('ssl').text if item.select_one('ssl') else 'N/A',
'requires' : item.select_one('requires \: ').text if item.select_one('requires \: ') else 'N/A',
'authentication' : item.select_one('authentication').text if item.select_one('authentication') else 'N/A',
'usernameincludesdomain' : item.select_one('usernameincludesdomain').text if item.select_one('usernameincludesdomain') else 'N/A'
}
data[name]['services'][str(i)] = service
i+=1
print(data)
在這里查看結構
如果您將xml轉換為類似json的結構,那么像untangle
這樣的庫可能會起作用?
如果只需要獲取名稱,則可以輕松使用BeautifulSoup
:
from bs4 import BeautifulSoup
s='''<domains>
<domain>
<name>zoznam.sk</name>
<description>Zoznam Slovakia</description>
<service>
<hostname>imap.zoznam.sk</hostname>
<port>143</port>
<protocol>IMAP</protocol>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
<service>
<hostname>smtp.zoznam.sk</hostname>
<port>587</port>
<protocol>SMTP</protocol>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
</domain>
<domain>
<name>123mail.org</name>
<description>123mail.org</description>
<service>
<hostname>imap.fastmail.com</hostname>
<port>993</port>
<protocol>IMAP</protocol>
<ssl/>
<requires/>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
<service>
<hostname>smtp.fastmail.com</hostname>
<port>587</port>
<protocol>SMTP</protocol>
<ssl/>
<requires/>
<authentication>PLAIN</authentication>
<usernameIncludesDomain/>
</service>
</domain>
<domain>
<name>Netvigator.com</name>
<description>netvigator.com</description>
<service>
<hostname>corpmail1.netvigator.com</hostname>
<port>995</port>
<protocol>POP</protocol>
<ssl/>
<authentication>NONE</authentication>
<usernameIncludesDomain/>
</service>
<service>
<hostname>corpmail1.netvigator.com</hostname>
<port>587</port>
<protocol>SMTP</protocol>
<ssl/>
<authentication>NONE</authentication>
<usernameIncludesDomain/>
</service>
</domain>
</domains>'''
soup = BeautifulSoup(s, 'html.parser')
configs = [n.text for n in soup.find_all('name')]
你會得到:
['zoznam.sk', '123mail.org', 'Netvigator.com']
要獲取每個服務的信息,可以添加以下代碼:
soup = BeautifulSoup(s, 'html.parser')
configs = {}
services = soup.find_all('service')
for serv in services:
hostname = serv.find('hostname').text
configs[hostname] = {}
configs[hostname]['port'] = serv.find('port').text
configs[hostname]['protocol'] = serv.find('protocol').text
configs[hostname]['auth'] = serv.find('authentication').text
你會得到configs
,它是字典的字典:
{'imap.zoznam.sk': {'port': '143', 'protocol': 'IMAP', 'auth': 'PLAIN'},
'smtp.zoznam.sk': {'port': '587', 'protocol': 'SMTP', 'auth': 'PLAIN'},
'imap.fastmail.com': {'port': '993', 'protocol': 'IMAP', 'auth': 'PLAIN'},
'smtp.fastmail.com': {'port': '587', 'protocol': 'SMTP', 'auth': 'PLAIN'},
'corpmail1.netvigator.com': {'port': '587', 'protocol': 'SMTP', 'auth': 'NONE'}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.