最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】PySpider中如何单个页面返回多个json数据结果

JSON crifan 3307浏览 0评论

折腾:

【已解决】PySpider如何把json结果数据保存到csv或excel文件中

期间,遇到个情况是:

PySpider保存单个dict的json字典对象,是可以用return去返回即可。

但是此处,单个page页面需要返回多个dict

而之前返回的是dict的list,结果导致保存出来的result的csv不是我们希望的对应那几列的效果。

所以要去搞清楚,单个页面,一次性,如何返回多个结果。

pyspider 单个页面 返回多个值

如何将多个页面的抓取结果存储于resultdb中的同一条记录中? · Issue #109 · binux/pyspider

Problem on combine information from different pages – Google Groups

请问pyspider中一个页面内如果要返回多条数据怎么做? – SegmentFault 思否

self.send_message – pyspider

【总结】

最后是利用send_message,将单个页面的多个结果,for循环后,每个dict结果,都调用send_message去发送message给自己的项目,在收到message的地方,再返回dict结果。

代码:

<code>    # @config(age=10 * 24 * 60 * 60)
    def picSeriesPage(self, response):
        ...
        allSerieDictList = []
        for curIdx, eachNameUrlDict in enumerate(fullModelNameUrlDictList):
            curSerieDict = {
                "品牌": mainBrandDict["text"],
                "子品牌": subBrandDict["text"],
                "车系": brandSerieDict["text"],
                "车型": eachNameUrlDict["车型"]
            }
            allSerieDictList.append(curSerieDict)
            print("before send_message: [%d] curSerieDict=%s" % (curIdx, curSerieDict))
            self.send_message(self.project_name, curSerieDict, url=eachNameUrlDict["url"])

        # print("allSerieDictList=", allSerieDictList)
        # return allSerieDictList

    def on_message(self, project, msg):
        print("on_message: msg=", msg)
        return msg
</code>

debug运行后还可以发现左边页面上的message还会显示红色数字,表示收到了多少个mesage:

转载请注明:在路上 » 【已解决】PySpider中如何单个页面返回多个json数据结果

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

    87 queries in 0.156 seconds, using 22.19MB memory