NaiboWang/EasySpider · warning · Exception

dont parse timezone format

Error message

dont parse timezone format

What it means

Identical to error 2 but in Examples/Sample Tasks with Python/desc_crawl.py:51. Same TimeUtil.convert_timezone / parse_timezone code duplicated across sample tasks. The 'dont parse timezone format' else branch is unreachable given the shipped regex; the observable failure for bad input is an AttributeError from None.groupdict() in parse_timezone.

Source

Thrown at Examples/Sample Tasks with Python/desc_crawl.py:51

        return {
            'symbol': symbol,
            'offset': offset
        }

    @classmethod
    def convert_timezone(cls, dt, timezone="+0"):
        """默认是utc时间,需要"""
        result = cls.parse_timezone(timezone)
        symbol = result['symbol']

        offset = result['offset']

        if symbol == '+':
            return dt + timedelta(hours=offset)
        elif symbol == '-':
            return dt - timedelta(hours=offset)
        else:
            raise Exception('dont parse timezone format')


def generate_timestamp():
    current_GMT = time.gmtime()
    # ts stores timestamp
    ts = calendar.timegm(current_GMT)

    current_time = datetime.utcnow()
    convert_now = TimeUtil.convert_timezone(current_time, '+8')
    print("current_time:    " + str(convert_now))
    return str(convert_now)


def main():
    # result = os.popen('python ServiceWrapper_ExecuteStage.py 38')
    # res = result.read()
    # for line in res.splitlines():
    #     print("\n\n\n\nfinename:\n\n\n\n\n", line)

View on GitHub (pinned to 191bd6d754)

Solutions

  1. Apply the same fix as error 2 to desc_crawl.py (and any other copy): validate timezone shape and guard the None regex result.
  2. Extract TimeUtil into a shared module and import it in both sample tasks to kill the duplication.
  3. Add a unit test for parse_timezone covering valid and invalid inputs.

Example fix

# before (desc_crawl.py)
result = re.match(r'(?P<symbol>[+-])(?P<offset>\d+)', timezone)
symbol = result.groupdict()['symbol']

# after
if not re.match(r'^[+-]\d+$', timezone or ''):
    raise ValueError(f'invalid timezone: {timezone!r}')
Defensive patterns

Strategy: validation

Validate before calling

import re
if not re.match(r'^[+-]\d+$', timezone or ''):
    raise ValueError(f'timezone must look like +8 / -5, got {timezone!r}')

Prevention

When it happens

Trigger: TimeUtil.convert_timezone(dt, timezone) where a modified parse_timezone returns a symbol other than '+' or '-'; or, in practice, parse_timezone crashes first on a non-matching timezone string.

Common situations: Copy-pasted sample task scripts drift from each other; desc_crawl.py carries the same latent bug as author_crawl.py. Bad timezone from config or CLI triggers the AttributeError variant.

Related errors


AI-assisted analysis of NaiboWang/EasySpider@191bd6d754 (2026-08-13). Data as JSON: /api/errors/67e7f167ff059c36. Report an issue: GitHub.