查找元素的特定兄弟姐妹

Find specific sibling of an element

我的 XML 页面结构如下:

<address>
<city>Anaheim</city>
<state>California</state>
<zip>92801</zip>
<country>United States</country>
</address>

<address>
<city>Berkley</city>
<state>California</state>
<zip>94705</zip>
<country>United States</country>
</address>

我只想获取城市标签的值,其中邮政编码值满足条件。 例如,我需要那些城市名称,其中 zip=92801.

python 中是否有简单的方法来做到这一点?

这将达到预期的效果:

my_string = '''
  <root>
    <address>
      <city>Anaheim</city>
      <state>California</state>
      <zip>92801</zip>
      <country>United States</country>
    </address>
    <address>
      <city>Berkley</city>
      <state>California</state>
      <zip>94705</zip>
      <country>United States</country>
    </address>
  </root>
'''

from lxml import etree

root = etree.fromstring(my_string)
cities = root.xpath('.//zip[text()="92801"]/preceding-sibling::city')

你用ElementTree怎么样

import xml.etree.ElementTree as ET  
tree = ET.parse('country_data.xml')
root = tree.getroot()

filtered_addresses = []
for address in root.findall('address'):
    if address.get('zip') == '92801':
        filtered_addresses.append(address)

如果您想改用 Beautiful Soup:

my_string = '''
<root>
  <address>
    <city>Anaheim</city>
    <state>California</state>
    <zip>92801</zip>
    <country>United States</country>
  </address>
  <address>
    <city>Berkley</city>
    <state>California</state>
    <zip>94705</zip>
    <country>United States</country>
  </address>
</root>
'''

from bs4 import BeautifulSoup
soup = BeautifulSoup(my_string, 'html.parser')
desired_zips = soup.findAll('zip', text="92801")
cities = []
for zip_tag in desired_zips:
    cities.append(zip_tag.findPreviousSibling('city'))

print(cities)

输出:

[<city>Anaheim</city>]

注意:您可以将此 for 循环写入列表推导式,但它看起来笨拙且不可读。