Python和XML出错

问题描述 投票:3回答:1

我在尝试从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等链接进行各种更改但我仍然遇到同样的错误。

python xml
1个回答
2
投票

而不是猜测文本编码和解码它的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
© www.soinside.com 2019 - 2024. All rights reserved.