通过'解析XML;元素树&x27;用Python

通过'解析XML;元素树&x27;用Python,python,xml,xml-parsing,elementtree,Python,Xml,Xml Parsing,Elementtree,我试图解析XML文件,但对名称空间和标记有点困惑。最后,我需要从中获取值 d:标题 d:日期 d:白天 d:晚上 d:Accuweather d:吉斯米特奥 有人能帮我处理一下密码吗。 谢谢 您可以创建别名的dict名称空间,以便于xpath查询 从xml.etree.cElementTree导入ElementTree tree=ElementTree(file=“file.xml”) root=tree.getroot() 名称空间={ “p”:”http://www.w3.org/200

我试图解析XML文件,但对名称空间和标记有点困惑。最后,我需要从中获取值

  • d:标题
  • d:日期
  • d:白天
  • d:晚上
  • d:Accuweather
  • d:吉斯米特奥
有人能帮我处理一下密码吗。
谢谢

您可以创建别名的dict名称空间,以便于xpath查询

从xml.etree.cElementTree导入ElementTree
tree=ElementTree(file=“file.xml”)
root=tree.getroot()
名称空间={
“p”:”http://www.w3.org/2005/Atom",
“m”:”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata",
“d”:”http://schemas.microsoft.com/ado/2007/08/dataservices",
}
xpath=“p:entry/p:content/m:properties”
res=root.findall(xpath,名称空间=名称空间)
值=[]
对于x英寸分辨率:
values.append(
{
“Title”:x.find(“d:Title”,名称空间=名称空间)。text,
“Date”:x.find(“d:Date”,名称空间=名称空间)。text,
“Day”:x.find(“d:Day”,名称空间=名称空间)。text,
“Night”:x.find(“d:Night”,名称空间=名称空间)。text,
“Accuweather”:x.find(“d:Accuweather”,名称空间=名称空间)。文本,
“Gismeteo”:x.find(“d:Gismeteo”,名称空间=名称空间)。text,
}
)
打印(值)
印刷品

[
{
“头衔”:“头衔”,
“日期”:“2020-02-12T00:00:00Z”,
“日”:“-8”,
“夜”:“-14”,
“Accuweather”:“挈挈挈挈挈挈”,
“Gismeteo”:“Пааааачаа,бааааааааа,
}
]

您得到了什么错误?没有错误,只是我得到了空列表看起来像(我没有测试)您只需要删除findall中的
m:
前缀…
root.findall('{http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}属性“
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="https://portal.erg.kz/weather/_api/">
       <id>0832f8fd-2ca1-4152-877b-3b28fc3eb1dc</id>
       <title />
       <updated>2020-02-12T08:53:05Z</updated>
       <entry m:etag="&quot;385&quot;">
          <id>Web/Lists(guid\'891430d2-9610-455c-be63-aa1bfd3c482f\')/Items(1)</id>
          <category term="SP.Data.WeatherInfoListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
          <link rel="edit" href="Web/Lists(guid\'891430d2-9610-455c-be63-aa1bfd3c482f\')/Items(1)" />
          <title />
          <updated>2020-02-12T08:53:05Z</updated>
          <author>
             <name />
          </author>
          <content type="application/xml">
             <m:properties>
                <d:Title>Лисаковск</d:Title>
                <d:Date m:type="Edm.DateTime">2020-02-12T00:00:00Z</d:Date>
                <d:Day m:type="Edm.Double">-8</d:Day>
                <d:Night m:type="Edm.Double">-14</d:Night>
                <d:Accuweather>Теплее</d:Accuweather>
                <d:Gismeteo>Переменная облачность, небольшой снег</d:Gismeteo>
                <d:Intellicast m:null="true" />
                <d:theWeatherChannel m:null="true" />
             </m:properties>
          </content>
       </entry>
...

url = 'https://portal.site.com/weather/_api/web/lists#'
r = requests.get(url, auth=HttpNtlmAuth('user','pass'))
xml_string = r.text
root = ET.fromstring(xml_string)
root.findall('{http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}m:properties')