子名称相同时如何解析XML

时间:2019-03-14 02:40:18

标签: python xml xml-parsing

因此,我的XML看起来像this,您将如何看到有多个address_component,而我需要最后一个,并附带高级代码。 您如何跳过所有内容,直到代码在数据中看到“国家”类型?

#XML

import urllib.request, urllib.parse, urllib.error
import xml.etree.ElementTree as ET
import ssl

api_key = False
# If you have a Google Places API key, enter it here
# api_key = 'AIzaSy___IDByT70'
# https://developers.google.com/maps/documentation/geocoding/intro

if api_key is False:
    api_key = 42
    serviceurl = 'http://py4e-data.dr-chuck.net/xml?'
else :
    serviceurl = 'https://maps.googleapis.com/maps/api/geocode/xml?'

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

while True:
    address = input('Enter location: ')
    if len(address) < 1: break

    parms = dict()
    parms['address'] = address
    if api_key is not False: parms['key'] = api_key
    url = serviceurl + urllib.parse.urlencode(parms)
    print('Retrieving', url)
    uh = urllib.request.urlopen(url, context=ctx)

    data = uh.read()
    print('Retrieved', len(data), 'characters')
    tree = ET.fromstring(data)

    results = tree.findall('result')

    location = results[0].find('address_component').find('short_name').text

    print(location)

1 个答案:

答案 0 :(得分:1)

尝试一下

import xml.etree.ElementTree as ET
import requests


# Look for the value of 'country'
XML = requests.get('http://py4e-data.dr-chuck.net/xml?address=Greenwood&key=42').content
root = ET.fromstring(XML)
results = root.findall('.//result')
for result in results:
    address_components = result.findall('.//address_component')
    for address_component in address_components:
        _type = address_component.find('type').text
        if _type == 'country':
            print(address_component.find('short_name').text)