[英]How to parse XML in Python
I have an XML retrieved from NOAA and I am trying to parse it using minidom in Python, but I am not able to retrieve the values. 我有一个从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>
` `
I need to retrieve the values under tag maximum temperature. 我需要检索标签最高温度下的值。
Using the BeautifulpSoup is an easy way. 使用BeautifulpSoup是一种简单的方法。
You can try. 你可以试试。 like this.
像这样。
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)
You can use Beautiful Soup with libxml. 您可以将Beautiful Soup与libxml一起使用。 Here is how to do proper setup tested for ubuntu 14.04:
以下是进行针对Ubuntu 14.04测试的正确设置的方法:
sudo apt-get install libxml2-dev libxslt1-dev lib32z1-dev python-dev -y
pip install lxml
pip install beautifulsoup4
Replace python-dev
with python3-dev
if you are using python3. 如果您使用的是python3,请用
python3-dev
替换python-dev
。 You can parse xml as follows: 您可以按以下方式解析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)
Please refer to documentation for further examples of finding elements. 请参阅文档以获取更多查找元素的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.