V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
360safe
V2EX  ›  Python

scrapy 批量写入,不足数量如何收尾

  •  
  •   360safe · 2017-07-04 10:27:20 +08:00 · 2047 次点击
    这是一个创建于 2481 天前的主题,其中的信息可能已经有所发展或是发生改变。
    请问:
    我在 Pipelines 的__init__中设置了一个变量 AAA,代码里写着:
    if len(AAA)> 500:
    汇总写入一次数据;
    AAA= []

    请问:最后程序结束的时候,如果 AAA 不足 500,如何收尾。
    7 条回复    2017-07-04 14:05:26 +08:00
    bazingaterry
        1
    bazingaterry  
       2017-07-04 11:01:22 +08:00 via iPhone
    结束的时候还有一个函数(一时之间想不起名字
    jigloo
        2
    jigloo  
       2017-07-04 11:06:18 +08:00
    判断一下当前时间,超过上次记录时间比如 1 分钟的话,即使不足 500 条也写入。算是最简单的 workaround 了。
    sikariba
        3
    sikariba  
       2017-07-04 11:21:07 +08:00
    就用一楼的那个,收尾的时候再写一次。closed 函数。
    https://docs.scrapy.org/en/latest/topics/spiders.html#scrapy.spiders.Spider.closed
    naomhan
        4
    naomhan  
       2017-07-04 11:21:52 +08:00   ❤️ 1
    pipline 里有个 close_spider 可以在这里导出
    1O
        5
    1O  
       2017-07-04 12:03:31 +08:00
    都是🐸,你看你都丑成啥样了。
    360safe
        6
    360safe  
    OP
       2017-07-04 14:05:17 +08:00
    谢谢大家,明白了,都怪自己没有细看官方文档。谢谢大家,学习成长了。
    360safe
        7
    360safe  
    OP
       2017-07-04 14:05:26 +08:00
    @1O 一样丑……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1029 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 19:14 · PVG 03:14 · LAX 12:14 · JFK 15:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.