您当前的位置: 首页 >  ui

默默爬行的虫虫

暂无认证

  • 2浏览

    0关注

    84博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

写入保存文件时出现错误TypeError: a bytes-like object is required, not ‘str‘

默默爬行的虫虫 发布时间:2022-07-11 16:43:02 ,浏览量:2

写入保存文件时出现错误TypeError: a bytes-like object is required, not ‘str’ 代码分析
    content = []

    title_0 = html_01.xpath('/html/body/div[1]/div[2]/div[1]/div[1]/div[2]/h1')
    for title_01 in title_0:
        title = title_01.xpath('./text()')[0]
        print(title)
        chapter = []

        select = html_01.xpath('//*[@id="play_0"]/ul/li[position()>6]')

        for select_01 in select:
            i_0 = select_01.xpath('./a/text()')[0]
            i_1 = select_01.xpath('./a/@href')[0]
            # print(i_0)
            # print(i_1)

            chapter.append({'href': i_1, 'chapter': i_0})
        content.append({'book': title, 'content': chapter})
    with open('daomubiji0.json', 'wb') as fp:  # 将所得的数据存储为json文件
        json.dump(content, fp=fp, ensure_ascii=False, indent=4, sort_keys=True)

在这里插入图片描述

解决方法:

将wb改为w 完美解决

关注
打赏
1658895887
查看更多评论
立即登录/注册

微信扫码登录

0.0453s