2017-08-20 22 views
1

大家好!我正在尝试创建一个使用Google Geocode API(XML)的应用程序。这是我一起工作的XML数据:如何提取与python中另一个标记具有相同名称的xml标记中的数据?

<GeocodeResponse> 
<status>OK</status> 
<result> 
    <type>establishment</type> 
    <type>point_of_interest</type> 
    <type>university</type> 
    <formatted_address>77 Massachusetts Ave, Cambridge, MA 02139, USA</formatted_address> 
    <address_component> 
    <long_name>77</long_name> 
    <short_name>77</short_name> 
    <type>street_number</type> 
    </address_component> 
    <address_component> 
    <long_name>Massachusetts Avenue</long_name> 
    <short_name>Massachusetts Ave</short_name> 
    <type>route</type> 
    </address_component> 
    <address_component> 
    <long_name>Area 2/MIT</long_name> 
    <short_name>Area 2/MIT</short_name> 
    <type>neighborhood</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>Cambridge</long_name> 
    <short_name>Cambridge</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>Middlesex County</long_name> 
    <short_name>Middlesex County</short_name> 
    <type>administrative_area_level_2</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>Massachusetts</long_name> 
    <short_name>MA</short_name> 
    <type>administrative_area_level_1</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>United States</long_name> 
    <short_name>US</short_name> 
    <type>country</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>02139</long_name> 
    <short_name>02139</short_name> 
    <type>postal_code</type> 
    </address_component> 
    <geometry> 
    <location> 
    <lat>42.3600910</lat> 
    <lng>-71.0941600</lng> 
    </location> 
    <location_type>ROOFTOP</location_type> 
    <viewport> 
    <southwest> 
    <lat>42.3587420</lat> 
    <lng>-71.0955090</lng> 
    </southwest> 
    <northeast> 
    <lat>42.3614400</lat> 
    <lng>-71.0928110</lng> 
    </northeast> 
    </viewport> 
    </geometry> 
    <place_id>ChIJh2oa9apw44kRPCAIs6WO4NA</place_id> 
</result> 
</GeocodeResponse> 

我想通过XML数据的提取县:

<address_component> 
    <long_name>Middlesex County</long_name> 
    <short_name>Middlesex County</short_name> 
    <type>administrative_area_level_2</type> 
    <type>political</type> 
    </address_component> 

然而,在XML数据的其他标记使用相同的“address_component”和“long_name”的名称。由于没有与这些标签相关的属性,我无法找到我想要的特定数据。任何人都可以请帮助我如何通过使用Python的XML数据,并找到我需要的确切数据,尽管标签具有相同的名称?

回答

0

如果你打算与子元素type=administrative_area_level_2得到address_component,你可以遍历XML并选择所需的元素:

find_by_tag("long_name") 
## 'Middlesex County' 

import xml.etree.ElementTree as ET 
root = ET.fromstring("your xml string") 

def find_by_tag(tag, add_type= "administrative_area_level_2"): 
    for address in root.iter("address_component"): 
     if address.find("type").text == add_type: 
      return address.find(tag).text 
    return None 

您可以通过使用功能find_by_tag得到long_name或其他标签,例如:

find_by_tag("short_name") 
## 'Middlesex County' 
find_by_tag("short_name", "postal_code") 
## '02139' 
+0

感谢您的信息!我发现了我一直试图获得的大量XML代码。但是,我如何使用它?我尝试了很多不同的查找函数来获取“long_name”标签,但它不起作用。看起来好像我对接下来做什么感到困惑,所以有人可以解释我应该如何继续使用这些数据!感谢迄今已回复的黄先生! –

+0

@RaamizAbbasi我编辑了我的答案。希望对你有效。 – Huang

相关问题