python中使用urllib2获取http请求状态码的代码例子,pythonurllib2
python中使用urllib2获取http请求状态码的代码例子,pythonurllib2
采集内容常需要得到网页返回的验证码做进一步处理
下面代码是用python写的用来获取网页http状态码的脚本
#!/usr/bin/python # -*- coding: utf-8 -*- #encoding=utf-8 #Filename:states_code.py import urllib2 url = 'http://www.bkjia.com/' response = None try: response = urllib2.urlopen(url,timeout=5) except urllib2.URLError as e: if hasattr(e, 'code'): print 'Error code:',e.code elif hasattr(e, 'reason'): print 'Reason:',e.reason finally: if response: response.close()
只有try无异常发生时,才会print 2,你说从不执行print 2,说明try代码块中有问题
非200的不一定会有源码。你可以参看RFC2616的说明,比如302、301你就去再请求header中Location的url,5xx你就可以放弃,等等……
相关内容
- python基于mysql实现的简单队列以及跨进程锁实例详解,
- Python编程语言的35个与众不同之处(语言特征和使用技
- Python中的并发编程实例,python并发编程实例
- python实现跨文件全局变量的方法,
- gearman的安装启动及python API使用实例,gearmanpython
- Python中的魔法方法深入理解,python深入理解
- Python中使用logging模块代替print(logging简明指南),py
- Python中__init__和__new__的区别详解,__init___new_
- python实现的一个火车票转让信息采集器,python采集器
- python实现的登录和操作开心网脚本分享,python实现脚本
评论关闭