【问题标题】:Error with Python and XMLPython 和 XML 错误
【发布时间】:2018-08-14 18:12:04
【问题描述】:

我在尝试从我的 XML 中获取值时遇到错误。我得到“不支持带有编码声明的 Unicode 字符串。请使用字节输入或没有声明的 XML 片段。”

这是我的代码:

import requests
import lxml.etree
from requests.auth import HTTPBasicAuth

r= requests.get("https://somelinkhere/folder/?parameter=abc", auth=HTTPBasicAuth('username', 'password'))
print r.text

root = lxml.etree.fromstring(r.text)
textelem = root.find("opensearch:totalResults")
print textelem.text

我收到此错误:

Traceback (most recent call last):
  File "tickets2.py", line 8, in <module>
    root = lxml.etree.fromstring(r.text)
  File "src/lxml/lxml.etree.pyx", line 3213, in lxml.etree.fromstring (src/lxml/lxml.etree.c:82934)
  File "src/lxml/parser.pxi", line 1814, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:124471)
ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.

这是 XML 的样子,我试图在最后一行抓取文件。

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:apple-wallpapers="http://www.apple.com/ilife/wallpapers" xmlns:g-custom="http://base.google.com/cns/1.0" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:cc="http://web.resource.org/cc/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:g-core="http://base.google.com/ns/1.0">
  <title>Feed from some link here</title>
  <link rel="self" href="https://somelinkhere/folder/?parameter=abc" />
  <link rel="first" href="https://somelinkhere/folder/?parameter=abc" />
  <id>https://somelinkhere/folder/?parameter=abc</id>
  <updated>2018-03-06T17:48:09Z</updated>
  <dc:creator>company.com</dc:creator>
  <dc:date>2018-03-06T17:48:09Z</dc:date>
  <opensearch:totalResults>4</opensearch:totalResults>

我尝试了对 https://twigstechtips.blogspot.com/2013/06/python-lxml-strings-with-encoding.htmlhttp://makble.com/how-to-parse-xml-with-python-and-lxml 等链接的各种更改,但我一直遇到同样的错误。

【问题讨论】:

  • 或许可以试试lxml.etree.fromstring(r.content)

标签: python xml python-requests lxml


【解决方案1】:

不要使用r.text,它会猜测文本编码并对其进行解码,而是尝试使用r.content,它将响应正文作为字节访问。 (见http://docs.python-requests.org/en/latest/user/quickstart/#response-content。)

您也可以使用r.raw。请参阅parsing XML file gets UnicodeEncodeError (ElementTree) / ValueError (lxml) 了解更多信息。

一旦该问题得到解决,您就会遇到命名空间问题。您尝试查找的元素 (opensearch:totalResults) 具有前缀 opensearch,它绑定到 uri http://a9.com/-/spec/opensearch/1.1/

您可以通过组合命名空间 uri 和本地名称(Clark 表示法)来找到该元素:

{http://a9.com/-/spec/opensearch/1.1/}totalResults

请参阅http://lxml.de/tutorial.html#namespaces 了解更多信息。

这是一个实现了两个更改的示例:

os = "{http://a9.com/-/spec/opensearch/1.1/}"

root = lxml.etree.fromstring(r.content)
textelem = root.find(os + "totalResults")
print textelem.text

【讨论】:

  • 太棒了。你发送的最后一部分就像一个魅力。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多