用Python3从XML中提取元素?

问题描述:

我正在尝试编写一个Python 3脚本,用于查询Web API并接收XML响应。响应看起来是这样的 -用Python3从XML中提取元素?

<?xml version="1.0" encoding="UTF-8"?> 
<ipinfo> 
    <ip_address>4.2.2.2</ip_address> 
    <ip_type>Mapped</ip_type> 
    <anonymizer_status/> 
    <Network> 
     <organization>level 3 communications inc.</organization> 
     <OrganizationData> 
    <home>false</home> 
     <organization_type>Telecommunications</organization_type> 
     <naics_code>518219</naics_code> 
    <isic_code>J6311</isic_code> 
     </OrganizationData>  
     <carrier>level 3 communications</carrier> 
     <asn>3356</asn> 
     <connection_type>tx</connection_type> 
     <line_speed>high</line_speed> 
     <ip_routing_type>fixed</ip_routing_type> 
     <Domain> 
     <tld>net</tld> 
     <sld>bbnplanet</sld> 
     </Domain> 
    </Network> 
    <Location> 
     <continent>north america</continent> 
     <CountryData> 
     <country>united states</country> 
     <country_code>us</country_code> 
     <country_cf>99</country_cf> 
     </CountryData> 
     <region>southwest</region> 
     <StateData> 
     <state>california</state> 
     <state_code>ca</state_code> 
     <state_cf>88</state_cf> 
     </StateData> 
     <dma>803</dma> 
     <msa>31100</msa> 
     <CityData> 
     <city>san juan capistrano</city> 
     <postal_code>92675</postal_code> 
     <time_zone>-8</time_zone> 
     <area_code>949</area_code> 
     <city_cf>77</city_cf> 
     </CityData> 
     <latitude>33.499</latitude> 
     <longitude>-117.662</longitude> 
    </Location> 
</ipinfo> 

这是我的代码至今 -

import urllib.request 
import urllib.error 
import sys 
import xml.etree.ElementTree as etree 

… 

try: 
    xml = urllib.request.urlopen(targetURL, data=None) 
except urllib.error.HTTPError as e: 
    print("HTTP error: " + str(e) + " URL: " + targetURL) 
    sys.exit() 

tree = etree.parse(xml) 
root = tree.getroot() 

的API查询工作,并通过调试,我可以看到所有信息的“根”变量中。我的问题是,我一直无法弄清楚如何从返回的XML中提取像ASN(<asn></asn>)这样的东西。一直以来,我一直在用各种各样的发现,findalls和所有其他种类的方法来对抗这一点,但没有能够解决这个问题。我认为我已经达到了无法看到所有树木的地步,而且我在互联网上找到的每个例子似乎都没有帮助。有人能告诉我一个代码片段,它可以从树结构中提取XML元素的内容吗?

非常感谢

我会建议使用Beautiful Soup

从xml-code中提取数据非常强大。

实施例:

from bs4 import BeautifulSoup 
soup = BeautifulSoup(targetURL) 

soup.find_all('asn') #Would return all the <asn></asn> tags found!