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

yipeiwu_com5年前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()

相关文章

聊聊Python中的pypy

聊聊Python中的pypy

PyPy是一个虚拟机项目,主要分为两部分:一个Python的实现和 一个编译器 PyPy的第一部分: 用Python实现的Python   其实这么说并不准确,准确得说应该是用rPy...

5款非常棒的Python工具

5款非常棒的Python工具

工欲善其事必先利其器,一个好的工具能让起到事半功倍的效果,Python社区提供了足够多的优秀工具来帮助开发者更方便的实现某些想法,下面这几个工具给我的工作也带来了很多便利,推荐给追求美好...

Python程序设计入门(3)数组的使用

1、Python的数组可分为三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素。定义方式:arr = [元素] (2) Tuple 固定的数组,一旦定义后,其...

遍历python字典几种方法总结(推荐)

如下所示: aDict = {'key1':'value1', 'key2':'value2', 'key3':'value3'} print '-----------dict---...

python装饰器代替set get方法实例

对于变量的访问和设置,我们可以使用get、set方法,如下: class student: def __init__(self,name): self.__name =...