【问题标题】:Scrapy shell debug doesn't show itemsScrapy shell 调试不显示项目
【发布时间】:2014-01-26 19:36:02
【问题描述】:

我正在使用 Scrapy 0.22。我有一个使用项目加载器来提取项目的蜘蛛。当我从 scrapy shell 运行蜘蛛时,我只看到包含 None 的调试消息,而不是我的项目加载器中的项目。

2014-01-26 20:33:08+0100 [ChatroomSpider] 调试:从 http://somedomain.com/?a=chat_rooms> 抓取 无

但是,如果我取消注释 #print itemline,我可以看到该项目按预期打印到标准输出。

蜘蛛:

class ChatroomSpider(BaseSpider):
    name = 'ChatroomSpider'
    allowed_domains = ['somedomain.com']
    start_urls = ['http://somedomain.com/?a=chat_rooms']

    def parse(self, response):
        selector = Selector(response)
        for chatroom_div in selector.xpath(r'id("body")/div[count(div) = 4 and div/div]'):
            loader = ChatroomLoader(chatroom_div)
            chatroom = loader.load_item()
            #print chatroom
            yield chatroom

加载器:

class ChatroomLoader(XPathItemLoader):

    default_item_class = ChatRoomItem

    name_in = Encode(encoding='utf-8')
    name_out = TakeFirst()

    description_in = StripAndEncode(encoding='utf-8')
    description_out = TakeFirst()

    datetime_in = StrpTime('%d.%m.%Y %H:%M:%S')
    datetime_out = TakeFirst()

    def __init__(self, room_selector):
        super(ChatroomLoader, self).__init__(selector=room_selector)

        self.add_xpath('name', r'div[1]/div/font/b/a/text()')
        self.add_xpath('description', r'div[2]/div/text()')
        self.add_xpath('users', r'div[3]/div/a/font/text()')
        self.add_xpath('datetime', r'id("copyright")/text()[4]', re=r'[0-3]?[0-9].[0-2][0-9].201[3-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]')

【问题讨论】:

  • 您在 ItemLoader 实例化后没有使用 .add_xpath().add_value() 调用吗?我不明白chatroom 如果你不这样做(或者你只提供了你的蜘蛛代码的一部分)怎么能有有意义的数据
  • @pault。好点子。我也添加了加载器的代码。
  • @prasopes 你在使用自定义管道吗?
  • @sardok 哦。我现在看到了问题。我的管道没有返回项目,但我从未注意到它直接将项目存储到数据库。愿意发布答案以便我接受吗?

标签: python python-2.7 scrapy


【解决方案1】:

如果您使用自己的管道,请确保从中返回项目。

有关管道的更多信息; http://doc.scrapy.org/en/latest/topics/item-pipeline.html

【讨论】:

    猜你喜欢
    • 2014-03-14
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多