【问题标题】:scrapy not printing out stacktrace on exceptionscrapy没有在异常时打印出stacktrace
【发布时间】:2015-11-15 22:19:57
【问题描述】:

是否有特殊机制强制scrapy打印出所有python异常/stacktrace。

我犯了一个简单的错误,导致列表属性错误,导致 AttributeError 没有完整显示在日志中 出现的是:

2015-11-15 22:13:50 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 264,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 40342,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2015, 11, 15, 22, 13, 50, 860480),
 'log_count/CRITICAL': 1,
 'log_count/DEBUG': 1,
 'log_count/INFO': 1,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'spider_exceptions/AttributeError': 1,
 'start_time': datetime.datetime(2015, 11, 15, 22, 13, 49, 222371)}

所以它显示 AttributeError 计数为 1,但没有告诉我在哪里以及如何,我不得不手动将 ipdb.set_trace() 放入代码中以找出错误的位置。 Scrapy 自己继续执行其他线程而不打印任何内容

ipdb>
AttributeError: "'list' object has no attribute 'match'"
> /Users/username/Programming/regent/regentscraper/spiders/regent_spider.py(139)request_listing_detail_pages_from_listing_id_list()
    138             volatile_props = ListingScanVolatilePropertiesItem()
--> 139             volatile_props['position_in_search'] = list_of_listing_ids.match(listing_id) + rank_of_first_item_in_page
    140

scrapy 设置

# -*- coding: utf-8 -*-

# Scrapy settings for regentscraper project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     http://doc.scrapy.org/en/latest/topics/settings.html
#     http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
#     http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html

import sys
import os
import django
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__name__), os.pardir)))

print sys.path

os.environ['DJANGO_SETTINGS_MODULE'] = 'regent.settings'
django.setup()  #new for Django 1.8



BOT_NAME = 'regentscraper'

SPIDER_MODULES = ['regentscraper.spiders']
NEWSPIDER_MODULE = 'regentscraper.spiders'


ITEM_PIPELINES = {
   'regentscraper.pipelines.ListingScanPipeline': 300,
}

【问题讨论】:

  • 为问题添加了设置
  • 不,没有帮助 - 只是尝试过。谢谢@alecxe
  • 我已将其精确定位到设置文件中的以下行(我需要它,因为我使用的是 Django 1.8) - django.setup() #new for Django 1.8 --- 删除此行开始记录追溯 - 不知道为什么
  • 请提供您的解决方案作为可以轻松找到并投票赞成的答案。

标签: python web-crawler scrapy


【解决方案1】:

我遇到了与上述相同的事件。 我的环境中使用了以下版本:

  • Django (1.11.4)
  • Scrapy (1.4.0)
  • scrapy-djangoitem (1.1.1)

我通过在scrapy中加载的dnango设置中添加“LOGGING_CONFIG = None”解决了这个问题。 我创建了一个新的 django 设置文件作为 settings_scrapy,内容如下:

mysite.settings_scrapy

try:
    from mysite.settings import *
    LOGGING_CONFIG = None
except ImportError:
    pass

然后,设置文件在scrapy的设置文件中加载为:

import sys
import os
import django
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings_scrapy'
django.setup()

之后,出现了蜘蛛和管道中异常的堆栈跟踪。

参考

https://docs.djangoproject.com/en/1.11/topics/logging/#disabling-logging-configuration

【讨论】:

  • 您可以在 django 项目的 settings.py 中将 LOGGING['disable_existing_loggers'] 设置为 False,而不是设置 LOGGING_CONFIG = None。
【解决方案2】:

在您的实际蜘蛛中,我无法找到堆栈跟踪的位置,看起来您正在尝试连接到一个项目定义中?

我敦促你也包括完全感冒的蜘蛛以及你的物品来帮助解决这个问题,尽管这是两年前的事情,所以我相信你已经继续前进或弄清楚了

正如堆栈跟踪所指出的,'"list" 对象没有属性 "match"',这要么是一个错误,因为您使用的 list 已经是 python 中的一个逻辑,如您所知.. . 似乎是罪魁祸首,因为堆栈跟踪告诉你列表没有属性名称匹配,所以它使用列表函数所以是的......

也可能是您必须在完整的蜘蛛代码中找到您的项目值,然后将其重新定义为列表?

为了更好地衡量,当使用单词列表时,除非使用其功能性逻辑,否则将您的“列表”命名为除...列表之外的任何其他内容?

【讨论】:

    猜你喜欢
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 2015-05-24
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    相关资源
    最近更新 更多