为了账号安全,请及时绑定邮箱和手机立即绑定

报错:TypeError: 'set' object is not subscriptable


class Htmloutputer(object):
    def __init__(self):
        self.datas=[]
    def collect_data(self,data):#收集数据
        if data is None:#如果数据是空的就返回
            return
        self.datas.append(data)#否则就往列表中添加data

    def output_json(self):#写出文件
        fout=open('9udata.json','w')

        fout.write('{')#0--
        fout.write('"code":200,')
        fout.write('"author":"wenruding",')
        fout.write('"list":[')#1--
        fout.write('{')#2--

        for data in self.datas:

            fout.write('"url":"%s",'% data['url'])

            fout.write('"title":"%s",' % data['title'].encode('utf8'))

            fout.write('"summary":"%s",' % data['summary'].encode('utf8'))

        fout.write('}')#--2
        fout.write(']')#--1
        fout.write('}')#--0



        fout.close()

输出结果:

"C:\Program Files (x86)\Python38-32\python.exe" D:/file/python/get9u/spider_main.py

craw 1:https://a.9game.cn/lolsy/4489275.html

craw 2:https://a.9game.cn/lolsy/4490449.html

craw 3:https://a.9game.cn/lolsy/4489389.html

craw 4:https://a.9game.cn/lolsy/3740915.html

craw 5:https://a.9game.cn/lolsy/4492319.html

Traceback (most recent call last):

  File "D:/file/python/get9u/spider_main.py", line 34, in <module>

    obj_spider.craw(root_url)

  File "D:/file/python/get9u/spider_main.py", line 29, in craw

    self.outputer.output_json()#调用output_json进行输出

  File "D:\file\python\get9u\html_outputer.py", line 22, in output_json

    fout.write('"url":"%s",'% data['url'])

TypeError: 'set' object is not subscriptable


进程已结束,退出代码 1


然后输出文件内容是:

{"code":200,"author":"wenruding","list":[{


正在回答

举报

0/150
提交
取消
Python开发简单爬虫
  • 参与学习       227652    人
  • 解答问题       1219    个

本教程带您解开python爬虫这门神奇技术的面纱

进入课程

报错:TypeError: 'set' object is not subscriptable

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信