【发布时间】:2014-03-06 22:16:52
【问题描述】:
我正在使用 Requests 检索 Atom 响应,但遇到了编码问题:
当我使用 curl 检索它时,它是正确的,显示 Ä:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:zapi="http://zotero.org/ns/api">
<title>The power broker : Robert Moses and the fall of New York</title>
(snip)
<content zapi:type="citation" type="xhtml">
<span xmlns="http://www.w3.org/1999/xhtml">(Robert Ä. Caro 1974)</span>
</content>
</entry>
但是当我在 Python 2.7.4 上使用 requests 2.2.1 检索它时,我得到了这个 unicode 响应:
import requests
r = requests.get(url)
r.text
u'<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:zapi="http://zotero.org/ns/api">
<title>The power broker : Robert Moses and the fall of New York</title>
(snip)
<content zapi:type="citation" type="xhtml">
<span xmlns="http://www.w3.org/1999/xhtml">(Robert \u0102\x84. Caro 1974)</span>
</content>
</entry>'
当然,将其编码为 utf-8 并不会返回我的 Ä。做什么?
【问题讨论】:
-
您可以添加您的
requests代码吗? -
@Drewnes 哎呀,对不起。完成。
-
改用
r.content。
标签: python unicode encoding utf-8 python-requests