鱼C论坛

 找回密码
 立即注册
查看: 492|回复: 1

.JSON文件无法显示中文

[复制链接]
发表于 2018-8-12 10:36:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import requests
from bs4 import BeautifulSoup
import json


def get_html_text(url):
    try:
        r = requests.get(url)
        r.raise_for_status()
        # r.encoding = 'utf-8'
        return r.text
    except Exception as e:
        print(e)
        return ''


def parse_url(html):
    soup = BeautifulSoup(html, 'lxml')
    content = []
    for mulu in soup.find_all(class_='mulu'):
        h2 = mulu.find('h2')
        if h2 != None:
            h2_title = h2.string
            list = []
            for a in mulu.find(class_='box').find_all('a'):
                herf = a.get('href')
                box_title = a.get('title')
                list.append({'herf': herf, 'box_title': box_title})
            content.append({'title': h2_title, 'content': list})
    with open('novel.json', 'w') as f:
        json.dump(content, fp=f, ensure_ascii=False, indent=4)


if __name__ == '__main__':
    url = 'http://seputu.com/'
    html = get_html_text(url)
    parse_url(html)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-8-12 10:38:30 | 显示全部楼层
没有ensure_ascii=False可以写入,但是无法显示中文。加了ensure_ascii=False之后会出现UnicodeEncodeError: 'gbk' codec can't encode character '\xe7' in position 1: illegal multibyte sequence


另外为什么不能以wb形式写入。用wb写入时出现TypeError: a bytes-like object is required, not 'str'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 08:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表