【问题标题】:Python requests xml: xml.parsers.expat.ExpatError: not well-formed (invalid token):Python 请求 xml: xml.parsers.expat.ExpatError: not well-formed (invalid token):
【发布时间】:2021-11-26 08:29:20
【问题描述】:

我在使用 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)

【问题讨论】:

  • A) 我希望您解析 md_response.content 而不是文本版本,并且 b) 您是否真的查看过响应内容(或您解析的任何内容),因为它可能 isn' t xml?
  • 更改为 md_response.content 解决了这个问题。谢谢。

标签: python xml python-requests xmltodict


【解决方案1】:

将 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    相关资源
    最近更新 更多