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()

相关文章

django小技巧之html模板中调用对象属性或对象的方法

django小技巧之html模板中调用对象属性或对象的方法

环境:依赖最初test2数据库            python3版本  ...

python 将数据保存为excel的xls格式(实例讲解)

python提供一个库 xlwt ,可以将一些数据 写入excel表格中,十分的方便。贴使用事例如下。 #引入xlwt模块(提前pip下载好) import xlwt #使用wor...

django中ORM模型常用的字段的使用方法

与数据类型相关的字段 CharField         作用:字符串字段, 用于较短的字符串.  &nb...

Django REST framework 视图和路由详解

Django REST framework 视图和路由详解

DRF中的Request 在Django REST Framework中内置的Request类扩展了Django中的Request类,实现了很多方便的功能--如请求数据解析和认证等。...

Python3调用微信企业号API发送文本消息代码示例

本文主要向大家分享了Python3调用微信企业号API发送文本消息示例的有关代码,具体如下: #!/usr/bin/env python # -*- coding:utf-8 -*-...