推荐学习书目
› 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
omg21
V2EX  ›  Python

在设置了 404 页面的网页,如何才能让连接报错?

  •  
  •   omg21 · Oct 26, 2016 · 2659 views
    This topic created in 3621 days ago, the information mentioned may be changed or developed.
    我在用 urllib.request 抓取网页时用了 try.....except Exception as e: ,让他出错就在屏幕显示出来,然后再继续下一个循环。
    可是现在碰到一个问题,就是有些页面不能连接后会转到 404 页面,但是这时就不会触发 except ,导致 urllib.request 会取到 404 页面的代码,后边的程序再分析这个 404 页面,当然分析不出来结果。
    像这样的情况应该如何解决?
    4 replies  •  2016-10-26 23:52:08 +08:00
    kamen
        1
    kamen  
       Oct 26, 2016
    不怎么懂 python ,分析出不来结果就丢弃啊
    franklinyu
        2
    franklinyu  
       Oct 26, 2016
    说明网站差, HTTP 协议的正确搞法不是「跳转到 404 页面」,而是返回「 404 代码和 404 页面」。碰到这种网站只好你自己手动检查跳转网址了
    karenn
        3
    karenn  
       Oct 26, 2016
    查看一下响应头部的状态码是什么
    nccers
        4
    nccers  
       Oct 26, 2016
    看一下 网页内容吗? 如果是 404 直接 raise 一个 exception 就行了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2626 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 04:14 · PVG 12:14 · LAX 21:14 · JFK 00:14
    ♥ Do have faith in what you're doing.