我在使用python 3执行以下代码时遇到错误,但在python 2上运行正常
template_content = <HTML data>
with NamedTemporaryFile(suffix='.html') as render_file:
render_file.write(template_content.encode('utf-8'))
render_file.seek(0)
stream = StringIO()
casperjs_capture(stream, url='file://%s' % os.path.abspath(render_file.name))
错误:
*** TypeError: string argument expected, got 'bytes'
发布于 2018-09-27 03:31:15
我刚刚完成了从StringIO到BytesIO的更改,它为我工作。要找到解决方案,几乎需要一天的时间。
template_content = <HTML data>
with NamedTemporaryFile(suffix='.html') as render_file:
render_file.write(template_content.encode('utf-8'))
render_file.seek(0)
stream = BytesIO()
casperjs_capture(stream, url='file://%s' % os.path.abspath(render_file.name))
发布于 2018-09-26 13:42:44
NamedTemporaryFile()
返回的文件对象显然是文本模式,因此在写入render_file
之前不应将template_content
编码为字节。
更改:
render_file.write(template_content.encode('utf-8'))
至:
render_file.write(template_content)
https://stackoverflow.com/questions/52510539
复制相似问题