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
penkchow
V2EX  ›  Python

我正在写一个爬虫(gevent+requests+redis-py),出现了一些问题,看看各位有啥好的解决方案没?

  •  1
     
  •   penkchow · 2014-06-05 11:12:30 +08:00 · 5651 次点击
    这是一个创建于 3605 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我的爬虫大致思想的是这样的,我想爬取某些列表页面上所有的列表url,有很多页,我遍历这些页面,然后抓去这些页面上的内容,当某个页面请求出错的时候,我就将它保存到一个数据库,下次从这个数据库里面把错误的取出来,然后再处理,这样一直循环,直到所有的都被处理完。不多说了,直接代码吧(更详细的问题描述见代码的注释):



    不知道各位对我这段代码有什么看法,或者吐槽也行,自己找了一些相关资料,成效不大。
    10 条回复    2014-06-05 15:07:59 +08:00
    jander
        1
    jander  
       2014-06-05 11:34:16 +08:00
    应该加上
    from gevent import monkey; monkey.patch_socket()
    penkchow
        2
    penkchow  
    OP
       2014-06-05 11:47:14 +08:00
    哦,我加了monkey.patch_all()就包括了吧,官网文档有说明: http://www.gevent.org/gevent.monkey.html
    @jander
    jander
        3
    jander  
       2014-06-05 12:10:45 +08:00
    哦,没看仔细。
    redis连接异常。你的代码使用redis.ConnectionPool, 其实redis可以直接连,内部已经使用pool实现:
    redis.StrictRedis(host='localhost', port=6379, db=0)
    你可以直接连试试。
    jsonline
        4
    jsonline  
       2014-06-05 12:11:47 +08:00
    每个月都有一个人来问爬虫的问题。
    penkchow
        5
    penkchow  
    OP
       2014-06-05 12:28:43 +08:00   ❤️ 1
    @jander 额,连接池不是比直接连并发性更好么?
    penkchow
        6
    penkchow  
    OP
       2014-06-05 12:29:25 +08:00
    @jsonline 每个月总有那么几天……有人问爬虫……
    jander
        7
    jander  
       2014-06-05 12:46:50 +08:00
    @penkchow 已经有缺省的连接池,不用你操心。
    penkchow
        8
    penkchow  
    OP
       2014-06-05 12:53:42 +08:00
    @jander 你的意思是因为我用了这个连接池才会出问题的
    jander
        9
    jander  
       2014-06-05 13:02:49 +08:00   ❤️ 1
    @penkchow 我的意思:你的redis连接有问题,试试不同的方案。
    penkchow
        10
    penkchow  
    OP
       2014-06-05 15:07:59 +08:00
    Okay,试试。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3658 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 04:58 · PVG 12:58 · LAX 21:58 · JFK 00:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.