python中使用urllib2获取http请求状态码的代码例子

yipeiwu_com6年前Python基础

采集内容常需要得到网页返回的验证码做进一步处理

下面代码是用python写的用来获取网页http状态码的脚本

#!/usr/bin/python
# -*- coding: utf-8 -*-
#encoding=utf-8
#Filename:states_code.py
 
import urllib2
 
url = '//www.jb51.net/'
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()

相关文章

使用pyqt 实现重复打开多个相同界面

一般采用的方法: self.window = Qdialog() # 实例化 self.window.show() # 显示界面 用这种方法只能打开一个界面,self使得默认只...

pandas删除行删除列增加行增加列的实现

创建df: >>> df = pd.DataFrame(np.arange(16).reshape(4, 4), columns=list('ABCD'), ind...

python 异常处理总结

       最近,做个小项目经常会遇到Python 的异常,让人非常头疼,故对异常进行整理,避免下次遇到异常不知所措,以下就...

Python 列表(List)操作方法详解

列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型。列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索...

python 类对象和实例对象动态添加方法(分享)

实例如下所示: class Person(): def __init__(self, name): self.name = name def print_name(self)...