qq_41780177 2018-12-10 05:59 采纳率: 0%
浏览 2196

python爬虫scrapy爬取了数据无法写入json

用scrapy成功爬取了商品数据,但是到目录下却发现数据文件没有创建,郁闷。。pipelines文件代码如下

import codecs
import json

class AutopjtPipeline(object):
    def _int_(self):
        self.file=codecs.open("77.json","wb",encoding="utf-8")
    def process_item(self, item, spider):
        for j in range(0,len(item["name"])):
            name = item["name"][j]
            price=item["price"][j]
            comnum = item["comnum"][j]
            link = item["link"][j]
            # 将当前页下第j个商品的name、price、comnum、link等信息处理一下,重新组合成一个字典
            goods = {"name": name, "price": price, "comnum": comnum, "link": link}
            # 将组合后的当前页中第j个商品的数据写入json文件
            i = json.dumps(dict(goods), ensure_ascii=False)
            line = i + '\n'
            self.file.write(line)
            # 返回item
        return item
    def close_spider(self,spider):
        self.file.close()

同时报错
Traceback (most recent call last):
File "c:\users\93422\appdata\local\programs\python\python35\lib\site-packages\twisted\internet\defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:\Users\93422\Desktop\python\autopjt\autopjt\pipelines.py", line 28, in close_spider
self.file.close()
AttributeError: 'AutopjtPipeline' object has no attribute 'file'

items文件代码以及爬虫代码都基本没问题,爬虫代码如下

```import scrapy
from autopjt.items import AutopjtItem
from scrapy.http import Request

class AutospdSpider(scrapy.Spider):
name = 'autospd'
allowed_domains = ['dangdang.com']
start_urls = ['http://category.dangdang.com/pg1-cid4003872-srsort_sale_amt_desc.html'
]

def parse(self, response):
    item=AutopjtItem()
    item['name']=response.xpath("//p[@class='name']/@title").extract()
    item['price']=response.xpath('//span[@class="price_n"]/text()').extract()
    item['link']=response.xpath('//p[@class="name"]/@href').extract()
    item['comnum']=response.xpath('//a[@ddclick]/text()').extract()
    yield item
    for i in range(1,20):
        url="http://category.dangdang.com/pg"+str(i)+"-cid4003872-srsort_sale_amt_desc.html"
        yield Request(url,callback=self.parse)
  • 写回答

2条回答 默认 最新

  • gammey 2018-12-11 03:47
    关注

    AttributeError: 'AutopjtPipeline' object has no attribute 'file'
    这个是说你的pipeline class没有file这个属性。
    你看下你的构造函数是不是没执行?

    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿