注意json的dump方法中的ensure_ascii参数的值:
- 默认为True,这样输出所有非ASCII编码的时候,就会采用"u\xxxxx"的形式
- 若改为False,则非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