在Python中,如何使用urllib查看网站是404还是200? [英] In Python, how do I use urllib to see if a website is 404 or 200?

查看:132
本文介绍了在Python中,如何使用urllib查看网站是404还是200?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过urllib获取标头的代码?

How to get the code of the headers through urllib?

推荐答案

getcode()方法(在python2.6中添加)返回与响应一起发送的HTTP状态代码,如果URL不是HTTP URL,则返回None.

The getcode() method (Added in python2.6) returns the HTTP status code that was sent with the response, or None if the URL is no HTTP URL.

>>> a=urllib.urlopen('http://www.google.com/asdfsf')
>>> a.getcode()
404
>>> a=urllib.urlopen('http://www.google.com/')
>>> a.getcode()
200

这篇关于在Python中,如何使用urllib查看网站是404还是200?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆