我使用request.get函数在互联网上获取搜索结果。我试着把它记录在日志里。但是我不知道为什么它不能显示编码的字符串
export=request.get(url="https://trains.ctrip.com/pages/booking/getTransferList",params=data,headers=headers)
logging.basicConfig(filename=rb"C:\Study\Other\Self-study\Code\newfile.log",
format='%(asctime)s %(message)s',
filemode='wb')
handler = logging.FileHandler(rb"C:\Study\Other\Self-study\Code\newfile.log", 'wb')
logger=logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
logging.info(export.content)
以下是输出的一部分
"transferList":[{"departStation":"\xe4\xb8\x8a\xe6\xb5\xb7\xe8\x99\xb9\xe6\xa1\xa5","arriveStation":"\xe5\x
但是当我使用json编写它的时候
f=open("C:\Study\Other\Self-study\Code\export.json","wb")
f.write(export.content)
f.close
我得到了正确的输出
"transferList":[{"departStation":"上海虹桥","arriveStation":"南昌西",
我知道导出是一个response类,而export.content是二进制的,我尝试了二进制写入或使用UTF_8编码写入,但两者都得到了相同的错误输出。在这种情况下,有没有什么方法可以让我得到正确的日志?
发布于 2020-05-14 03:46:32
正如这里所描述的,https://stackoverflow.com/a/57004134/13362865看起来您需要告诉处理程序将内容编码为UTF-8。这是通过向FileHandler()创建传递encoding='utf-8'
来完成的。我还没有测试过这一点,但这似乎是合理的,它应该可以工作。
https://stackoverflow.com/questions/61783157
复制相似问题