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

网站爬虫爬着爬着程序就假死了,也不报错,也不继续,就卡着不动了,是哪里设计不对,求各位指教

  •  
  •   wsds · 2018-06-10 20:25:45 +08:00 · 6319 次点击
    这是一个创建于 2118 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用于网站爬图 代码太多,贴关键部分,应该问题就出自以下爬虫部分:

    for i in soup1.find_all("input", type="image"):
    	imgnow = time.strftime("%Y%m%d%H%M%S")
    	imgurl = i['data-src']
    	rname = imgurl .split('/')[-1]
    	opener = urllib.request.build_opener()
    	opener.addheaders = self.UA
    	urllib.request.install_opener(opener)
    
            try:
              #就是卡这里不动了,没有任何动静,程序静止了
    	  urllib.request.urlretrieve(
    	  imgurl, "./Pic/%s" % imgnow + "_" + str(rname))     
    
           except Exception as e:
    		print("出错了:%s,继续..." % e)
    	        continue
    
    16 条回复    2018-06-14 17:13:14 +08:00
    a7a2
        1
    a7a2  
       2018-06-10 20:32:46 +08:00
    需要加入超时处理,免得在对方网站限制或关闭时候有反应
    wsds
        2
    wsds  
    OP
       2018-06-10 20:43:42 +08:00
    @a7a2 怎么加,求指教
    bihuchao
        3
    bihuchao  
       2018-06-10 20:44:59 +08:00
    ThirdFlame
        4
    ThirdFlame  
       2018-06-10 20:50:23 +08:00
    推荐 requests。 当然了 不管用啥 都得考虑超时
    wsds
        5
    wsds  
    OP
       2018-06-10 20:58:19 +08:00
    @ThirdFlame 加了超时,现在可以抛出异常了,但老是有这种,放在 try 了,10 个请求,有 5 个就是这种,遇到就 pass 了继续。。。。:

    [WinError 10054] 远程主机强迫关闭了一个现有的连接。,继续...



    要么就干脆直接报错程序 终止了:
    requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))
    ThirdFlame
        6
    ThirdFlame  
       2018-06-10 21:17:14 +08:00
    @wsds ConnectionReset,对端发了 reset。原因很多,网络不好 对方等待超时,对方发现你再爬虫,所以超时。 各种原因。 想要仔细分析原因 抓包分析,看看是网络不好,还是对方主动的。
    wsds
        7
    wsds  
    OP
       2018-06-10 21:32:52 +08:00
    @ThirdFlame 换了网络,不像是网络问题,总之感觉爬的很费力,各种异常,try 都 try 不住
    SimbaPeng
        8
    SimbaPeng  
       2018-06-10 21:41:34 +08:00 via Android
    必须要 ip 代理
    herozhang
        9
    herozhang  
       2018-06-10 21:59:08 +08:00
    python 有个叫做 retry 的库,可以试一下,很方便的。
    mumbler
        10
    mumbler  
       2018-06-10 22:06:02 +08:00
    要加重试机制,任何网络都会不稳定,对方服务器也可能出现故障,屏蔽等情况
    wsds
        11
    wsds  
    OP
       2018-06-10 22:23:57 +08:00
    @herozhang 好的,我试一下
    @mumbler 确实要加重啊,不然跑几分钟就直接崩了
    Leigg
        12
    Leigg  
       2018-06-10 22:33:18 +08:00 via iPhone
    骚年,你得学会 requests。
    你说的假死问题应该出在你的代码上,好好排查。
    alvin666
        13
    alvin666  
       2018-06-10 22:34:37 +08:00 via Android
    为什么不用 requests ? requests.get(timeout=10)
    WWd0g
        14
    WWd0g  
       2018-06-11 00:14:38 +08:00
    @alvin666 这个没啥用,只针对链接超时,读取超时得这样写 timeout=(3,6)
    不过这样写了也没卵用~
    wsds
        15
    wsds  
    OP
       2018-06-11 09:34:10 +08:00
    @Leigg 请求地址用的就是 requests,只不过下载图片用的是 urllib.request.urlretrieve
    beforeuwait
        16
    beforeuwait  
       2018-06-14 17:13:14 +08:00
    我以前抓携程的时候也遇到这种问题
    程序就假死在那了。

    后来,我做了一个断点续爬来解决这个问题。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4064 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 05:17 · PVG 13:17 · LAX 22:17 · JFK 01:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.