【发布时间】:2016-03-11 03:23:26
【问题描述】:
我希望能够在我的 Python 代码中验证从模板渲染函数生成的一些 html。
我去Github page for validator.w3.org咨询API。
根据我对所读内容的解释,我尝试了以下代码:
import requests
import urllib
index_html = '<!DOCTYPE html>\n<html lang="en">\n<head>\n '\
'<meta charset="UTF-8">\n '\
'<title></title>\n</head>\n<body>\n \n</body>\n</html>\n'
FRAGMENT = ''
query = {}
QUERY = 3
tokens = ['https', 'validator.w3.org', 'nu/', query, FRAGMENT]
headers = {'Content-type': 'text/html; charset=utf-8'}
query = {'out': 'json'}
query = urllib.parse.urlencode(query)
tokens[QUERY] = query
url = urllib.parse.urlunsplit(tokens)
kwargs = dict(
headers=headers,
data=index_html,
)
response = requests.post(url, **kwargs)
response.json() 返回:
*** UnicodeEncodeError: 'ascii' codec can't encode character '\u201c' in position 48: ordinal not in range(128)
response.content是这个:
b'{"messages":[{"type":"info","message":"Content-Type 是 \xe2\x80\x9ctext/html\xe2\x80\x9d。使用 HTML 解析器。 "},{"type":"info","message":"使用支持 SVG 1.1、MathML 3.0、RDFa 1.1 和 ITS 2.0 的 HTML 架构。"},{"type":"error"," lastLine":5,"lastColumn":17,"firstColumn":10,"message":"元素 \xe2\x80\x9ctitle\xe2\x80\x9d 不能为空。","extract":"\n \ n
type(response.content) 是<class 'bytes'>。
我知道json.loads 需要一个字符串,所以我假设response.json 抛出异常,因为内容是无法解码为字符串的字节:
import json
json.loads(response.content.decode('utf-8'))
确实,同样的例外:
*** UnicodeEncodeError: 'ascii' codec can't encode character '\u201c' in position 48: ordinal not in range(128)
我的知识已经用完了,这让我一直想知道要更改这段代码的哪一部分才能从 requests.post response 获取 JSON。
提前感谢您的帮助。
【问题讨论】:
-
在使用 Python 2.x 而不是 Python 3.x 时,这似乎是一个问题;其他人可以确认吗?
-
噢!你的评论让我想起我一直在我的 virtualenv 中摆弄 Python 版本。我刚刚在 python3.4 中尝试了我的代码并达到了预期的结果。谢谢。 :-)
标签: python python-requests python-3.4