注意json的dump方法中的ensure_ascii參數(shù)的值:
- 默認(rèn)為True,這樣輸出所有非ASCII編碼的時(shí)候,就會(huì)采用"u\xxxxx"的形式
- 若改為False,則非ASCII編碼的字符就會(huì)按照原本的字符形式輸入
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