0

Scrapy 将时间向后显示 2 小时。值 2013-05-30 14:50:54 +0200 与 'start_time' 不同: datetime.datetime(2013, 5, 30, 12, 50, 53 , 860000)

2013-05-30 14:50:54+0200 [myspider] INFO: Dumping Scrapy stats:
  {'downloader/request_bytes': 120,
  'downloader/request_count': 1,
  'downloader/request_method_count/GET': 1,
  'downloader/response_bytes': 382,
  'downloader/response_count': 1,
  'downloader/response_status_count/200': 1,
  'finish_reason': 'finished',
  'finish_time': datetime.datetime(2013, 5, 30, 12, 50, 53, 887000),
  'httpcache/hits': 1,
  'log_count/DEBUG': 7,
  'log_count/INFO': 4,
  'response_received_count': 1,
  'scheduler/dequeued': 1,
  'scheduler/dequeued/memory': 1,
  'scheduler/enqueued': 1,
  'scheduler/enqueued/memory': 1,
  'start_time': datetime.datetime(2013, 5, 30, 12, 50, 53, 860000)}
2013-05-30 14:50:54+0200 [myspider] INFO: Spider closed (finished)

在我的蜘蛛中,我有一个使用 datetime.datetime.now() 的变量,并且正确地分配了这个变量的时间戳。

是否可以在scrapy中设置时区?

4

1 回答 1

0

Scrapy 将时间向后显示 2 小时。

如您所说,scrapy 仅以不同的格式显示时间,但日期时间值是相同的。

和你说的不一样。

2013-05-30 14:50:54+0200是相同的2013-05-30 12:50:54+0000

更新:

所以你想用本地时区打印日期时间。查看scrapy源码可以看到它使用了twisted logging,它具有以下功能:

def formatTime(self, when):
    """
    Format the given UTC value as a string representing that time in the
    local timezone.

    By default it's formatted as a ISO8601-like string (ISO8601 date and
    ISO8601 time separated by a space). It can be customized using the
    C{timeFormat} attribute, which will be used as input for the underlying
    L{datetime.datetime.strftime} call.

    @type when: C{int}
    @param when: POSIX (ie, UTC) timestamp for which to find the offset.

    @rtype: C{str}
    """
    if self.timeFormat is not None:
        return datetime.fromtimestamp(when).strftime(self.timeFormat)

    tzOffset = -self.getTimezoneOffset(when)
    when = datetime.utcfromtimestamp(when + tzOffset)
    tzHour = abs(int(tzOffset / 60 / 60))
    tzMin = abs(int(tzOffset / 60 % 60))
    if tzOffset < 0:
        tzSign = '-'
    else:
        tzSign = '+'
    return '%d-%02d-%02d %02d:%02d:%02d%s%02d%02d' % (
        when.year, when.month, when.day,
        when.hour, when.minute, when.second,
        tzSign, tzHour, tzMin)
于 2013-05-30T14:53:52.547 回答