如何在 Python 中解析 XML

How to parse XML in Python

我有一个从 NOAA 检索到的 XML,我试图在 Python 中使用 minidom 解析它,但我无法检索到这些值。

 `<parameters applicable-location="point1">
  <temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
    <name>Daily Maximum Temperature</name>
    <value>75</value>
    <value>67</value>
    <value>65</value>
    <value>72</value>
    <value>65</value>
    <value>64</value>
    <value>62</value>
  </temperature>
</parameters>

`

我需要检索标签最高温度下的值。

使用 BeautifulpSoup 是一种简单的方法。

你可以试试。像这样。

from bs4 import BeautifulSoup

XML_STRING = """
<parameters applicable-location="point1">
  <temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
    <name>Daily Maximum Temperature</name>
    <value>75</value>
    <value>67</value>
    <value>65</value>
    <value>72</value>
    <value>65</value>
    <value>64</value>
    <value>62</value>
  </temperature>
</parameters>
"""

soup = BeautifulSoup(XML_STRING, 'html.parser')
for tag in soup.find_all('value'):
    print(tag.string)

您可以将 Beautiful Soup 与 libxml 一起使用。以下是针对 ubuntu 14.04 测试的正确设置方法:

sudo apt-get install libxml2-dev libxslt1-dev lib32z1-dev python-dev -y
pip install lxml
pip install beautifulsoup4

如果您使用 python3,请将 python-dev 替换为 python3-dev。您可以按如下方式解析 xml:

file_content = """your xml string here"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(file_content, "xml")
max_temp_list = [int(item.string) for item in soup.find("temperature", {"type": "maximum"}).findAll("value")]
print(max_temp_list)

有关查找元素的更多示例,请参阅documentation