我希望能够验证Python代码中模板渲染函数生成的一些html。
我访问了validator.w3.org的Github页面,查阅了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"编解码器无法对位置48中的字符"\u201c"进行编码:序号不在(128)范围内
response.content
是这样的:
b'{"messages":[{"type":"info","message":"Content type was \xe2\x80\x9ctext/html/xe2\x80\x9d.Using The html parser."},{"type":"info","message":"Using The schema for html with SVG 1.1,MathML 3.0,RDFa 1.1,and ITS 2.0 support."}0\x9d不能为空。","extract":"\n\n
type(response.content)
是<class 'bytes'>
。我知道json.loads
需要一个字符串,所以我假设response.json
抛出了一个异常,因为内容以字节为单位,无法解码为字符串:
import json
json.loads(response.content.decode('utf-8'))
事实上,同样的例外:
***UnicodeEncodeError:"ascii"编解码器无法对位置48中的字符"\u201c"进行编码:序号不在(128)范围内
我的知识已经用完了,这让我一直在想,为了从requests.post
response
获得JSON,应该更改代码的哪一部分。
提前感谢您的帮助。
3。x x p y y T h o o n 2。p y y T h o n 3 x!
请参阅下面的更新
谢谢。
{'messages': [{'message': 'The Content-Type was “text/html”. Using the HTML parser.',
'type': 'info'},
{'message': 'Using the schema for HTML with SVG 1.1, MathML 3.0, RDFa 1.1, and ITS 2.0 support.',
'type': 'info'},
{'extract': 'n <title></title>n</hea',
'firstColumn': 10,
'hiliteLength': 8,
'hiliteStart': 10,
'lastColumn': 17,
'lastLine': 5,
'message': 'Element “title” must not be empty.',
'type': 'error'}]}
更新:
这个故事还有更多。事实上,我在使用Python3。我只是省略了关于使用py.test和--pdb选项的部分。
我怎么知道我在用Python3?
来自python3 test_mytest.py
的输出,其中内部test_mytest.py为:
if __name__ == '__main__':
import sys
sys.exit(pytest.main('-s --pdb'))
这是吗
平台linux-Python 3.4.3,pytest-2.8.3,py-1.4.31,pluggy-0.3.1
在加入pdb后,我仍然会遇到编码错误。我在@daveapp的回复中找到了解决方案。
他写了一页关于他在这个问题上的痛苦经历的文章。谢谢@daveapp。
一旦我执行了export PYTHONIOENCODING='utf_8'
,我就不再有任何编码错误了。
我弄错了我的错误!