如何将一个非ASCII编码的字典格式数据按照中文输入一个文件

2017-07-19  本文已影响0人  Philosopher_

注意json的dump方法中的ensure_ascii参数的值:

import requests
import json


headers = {
        'Pragma': 'no-cache',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Referer': 'http://bbs.cloud.icybee.cn/board/JobInfo',
        'Connection': 'keep-alive',
        'Cache-Control': 'no-cache',
    }
resp = requests.get("https://bbs.tju.edu.cn/api/board/189/page/", headers=headers)
# soup = BeautifulSoup(resp.text, 'lxml')
resp.encoding = 'utf-8'
print(type(resp.json()))

with open('index.json', "a+") as f:
    json.dump(resp.json(), f, ensure_ascii=False)

文件来源:http://python.usyiyi.cn/translate/python_278/library/json.html#module-json

上一篇 下一篇

猜你喜欢

热点阅读