python scrapy crawl xxx -o xx.json json中没有数据

python scrapy crawl xxx -o xx.json json中没有数据

有两个原因
没有配置管道文件,在setting.py中配置
其一:

# 管道文件配置,BossPipeline是pipelines.py中的类名
ITEM_PIPELINES = {
   'boss.pipelines.BossPipeline': 300,
}

其二:爬虫文件中 def parse(self, response):没有返回值

    def parse(self, response):
         # 匹配所有工作相关信息
        job_list = response.xpath('//div[@class="info-primary"]')
         items_list = []

        for info in job_list:
            # Item对象用来保存数据的
            item = BossItem()
            '''
            工作相关信息
            '''
            # 岗位名称
            # 加extract(),提取xpath匹配的内容,不加extract(),匹配的是对象
            name = info.xpath('./h3/a/div[@class="job-title"]/text()').extract()
            # 待遇
            money = info.xpath('./h3/a/span[@class="red"]/text()').extract()
            item['name'] = name[0]
            item['money'] = money[0]
            items_list.append(item)
            #必须要有返回值,把结果返回给管道处理
        return items_list

猜你喜欢

转载自blog.csdn.net/ithongchou/article/details/83862732