首页 > 解决方案 > Python 请求 xml: xml.parsers.expat.ExpatError: not well-formed (invalid token):

问题描述

我在使用 xmltodict 解析 XML 时遇到问题。此方法适用于其他在线 XML 文件,但现在我收到此错误:“xml.parsers.expat.ExpatError: not well-formed (invalid token):”

这里出了什么问题,我该如何解决/解决它?

import requests
import xmltodict

metadataxml = "https://nedlasting.geonorge.no/geonorge/Tjenestefeed_daglig.xml"

md_response = requests.get(metadataxml, stream=True)
md_data = md_response.text

xml = xmltodict.parse(md_data)

标签: pythonxmlpython-requestsxmltodict

解决方案


将 response.text 更改为 response.content 似乎可以解决问题。

metadataxml = "https://nedlasting.geonorge.no/geonorge/Tjenestefeed_daglig.xml"

md_response = requests.get(metadataxml, stream=True)
md_data = md_response.text

xml = xmltodict.parse(md_data)

推荐阅读