python网页请求urllib2模块简单封装代码


Posted in Python onFebruary 07, 2014

对python网页请求模块urllib2进行简单的封装。

例子:

#!/usr/bin/python
#coding: utf-8
import base64
import urllib
import urllib2
import time
class SendRequest:
  '''
  This class use to set and request the http, and get the info of response.
  e.g. set Authorization Type, request tyep..
  e.g. get html content, state code, cookie..
  SendRequest('http://10.75.0.103:8850/2/photos/square/type.json', 
              data='source=216274069', type='POST', auth='base',
     user='zl2010', password='111111')
  '''
  def __init__(self, url, data=None, type='GET', auth=None, user=None, password=None, cookie = None, **header):
    '''
    url:request, raise error if none
    date: data for post or get, must be dict type
    type: GET, POST
    auth: option, if has the value must be 'base' or 'cookie'
    user: user for auth
    password: password for auth
    cookie: if request with cookie 
    other header info: 
    e.g. referer='www.sina.com.cn'    
    '''
    self.url = url
    self.data = data
    self.type = type
    self.auth = auth
    self.user = user
    self.password = password 
    self.cookie = cookie 
    if 'referer' in header:
      self.referer = header[referer]
    else:
      self.referer = None
    if 'user-agent' in header:
      self.user_agent = header[user-agent]
    else:
      self.user_agent = None
    self.setup_request()
    self.send_request() 
  def setup_request(self):
    '''
    setup a request 
    '''
    if self.url == None or self.url == '':
      raise 'The url should not empty!'
    # set request type 
    #print self.url
    #print self.type
    #print self.data
    #print self.auth
    #print self.user
    #print self.password  
    if self.type == 'POST': 
      self.Req = urllib2.Request(self.url, self.data)
    elif self.type == 'GET':
      if self.data == None:
          self.Req = urllib2.Request(self.url)
      else:
        self.Req = urllib2.Request(self.url + '?' + self.data)
    else:
      print 'The http request type NOT support now!'
    ##set auth type 
    if self.auth == 'base':
      if self.user == None or self.password == None:
        raise 'The user or password was not given!'
      else:
        auth_info = base64.encodestring(self.user + ':' + self.password).replace('\n','')
        auth_info = 'Basic ' + auth_info 
        #print auth_info  
        self.Req.add_header("Authorization", auth_info)
    elif self.auth == 'cookie':
      if self.cookie == None:
        raise 'The cookie was not given!'
      else:
        self.Req.add_header("Cookie", self.cookie) 
    else:
      pass    ##add other auth type here 
    ##set other header info 
    if self.referer:
      self.Req.add_header('referer', self.referer)
    if self.user_agent:
      self.Req.add_header('user-agent', self.user_agent)
      
  def send_request(self):  
    '''
    send a request 
    '''
    # get a response object 
    try:
      self.Res = urllib2.urlopen(self.Req)
      self.source = self.Res.read()
      self.goal_url = self.Res.geturl()
      self.code = self.Res.getcode()
      self.head_dict = self.Res.info().dict
      self.Res.close()
    except urllib2.HTTPError, e:
      self.code = e.code
      print e
        
  def get_code(self):
    return self.code
  def get_url(self):
    return self.goal_url
  def get_source(self):        
    return self.source
  def get_header_info(self):
    return self.head_dict
  def get_cookie(self):
    if 'set-cookie' in self.head_dict:
      return self.head_dict['set-cookie']
    else:
      return None    
  def get_content_type(self):
    if 'content-type' in self.head_dict:
      return self.head_dict['content-type']
    else:
      return None
  def get_expires_time(self):
    if 'expires' in self.head_dict:
      return self.head_dict['expires']
    else:
      return None    
  def get_server_name(self):
    if 'server' in self.head_dict:
      return self.head_dict['server']
    else:
      return None   
  def __del__(self):
    pass   
__all__ = [SendRequest,]
if __name__ == '__main__':
  '''
  The example for using the SendRequest class 
  '''
  value = {'source':'216274069'}
  data = urllib.urlencode(value)
  url = 'http://10.75.0.103:8850/2/photos/square/type.json'
  user = 'wz_0001'
  password = '111111'
  auth = 'base'
  type = 'POST'
  t2 = time.time()
  rs = SendRequest('http://www.google.com')
  #rs = SendRequest(url, data=data, type=type, auth=auth, user=user, password=password)
  print 't2: ' + str(time.time() - t2)
  print '---------------get_code()---------------'
  print rs.get_code()
  print '---------------get_url()---------------'
  print rs.get_url()
  print '---------------get_source()---------------'
  print rs.get_source()
  print '---------------get_cookie()---------------'
  print rs.get_cookie()
  rs = None
Python 相关文章推荐
Python中的日期时间处理详解
Nov 17 Python
python解决汉字编码问题:Unicode Decode Error
Jan 19 Python
python构建自定义回调函数详解
Jun 20 Python
python多进程提取处理大量文本的关键词方法
Jun 05 Python
浅谈pandas用groupby后对层级索引levels的处理方法
Nov 06 Python
scrapy-redis的安装部署步骤讲解
Feb 27 Python
Python2与Python3关于字符串编码处理的差别总结
Sep 07 Python
通过代码实例了解Python3编程技巧
Oct 13 Python
Python实现曲线拟合的最小二乘法
Feb 19 Python
python解决OpenCV在读取显示图片的时候闪退的问题
Feb 23 Python
教你怎么用PyCharm为同一服务器配置多个python解释器
May 31 Python
Python matplotlib多个子图绘制整合
Apr 13 Python
python解析xml模块封装代码
Feb 07 #Python
python 解析XML python模块xml.dom解析xml实例代码
Feb 07 #Python
python合并文本文件示例
Feb 07 #Python
python实现哈希表
Feb 07 #Python
python处理cookie详解
Feb 07 #Python
urllib2自定义opener详解
Feb 07 #Python
python解析html开发库pyquery使用方法
Feb 07 #Python
You might like
php下通过伪造http头破解防盗链的代码
2010/07/03 PHP
色色整理的PHP面试题集锦
2012/03/08 PHP
PHP Parse Error: syntax error, unexpected $end 错误的解决办法
2012/06/05 PHP
Yii框架关联查询with用法分析
2014/12/02 PHP
php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法
2015/10/20 PHP
基于jquery实现图片广告轮换效果代码
2011/07/07 Javascript
js模仿jquery的写法示例代码
2013/06/16 Javascript
node.js+Ajax实现获取HTTP服务器返回数据
2014/11/26 Javascript
javascript二维数组转置实例
2015/01/22 Javascript
Jquery easyui开启行编辑模式增删改操作
2016/01/14 Javascript
JS文件上传神器bootstrap fileinput详解
2021/01/28 Javascript
js cookie实现记住密码功能
2017/01/17 Javascript
vue动态生成dom并且自动绑定事件
2017/04/19 Javascript
Js实现中国公民身份证号码有效性验证实例代码
2017/05/03 Javascript
如何更好的编写js async函数
2018/05/13 Javascript
echarts设置图例颜色和地图底色的方法实例
2018/08/01 Javascript
深入浅析Node环境和浏览器的区别
2018/08/14 Javascript
Node.js实现用户评论社区功能(体验前后端开发的乐趣)
2019/05/09 Javascript
Vue-CLI与Vuex使用方法实例分析
2020/01/06 Javascript
js中!和!!的区别与用法
2020/05/09 Javascript
浅析Python的Django框架中的Memcached
2015/07/23 Python
Python中的二维数组实例(list与numpy.array)
2018/04/13 Python
pandas多级分组实现排序的方法
2018/04/20 Python
Python绘制三角函数图(sin\cos\tan)并标注特定范围的例子
2019/12/04 Python
python 写一个文件分发小程序
2020/12/05 Python
LN-CC英国:伦敦时尚生活的缩影
2019/09/01 全球购物
Java servlet面试题
2012/03/04 面试题
综合素质的自我鉴定
2013/10/07 职场文书
校园广播稿500字
2014/02/04 职场文书
学生会部长竞聘书
2014/03/31 职场文书
银行行长竞聘演讲稿
2014/04/23 职场文书
2015年酒店工作总结范文
2015/04/07 职场文书
通知函格式范文
2015/04/27 职场文书
2019企业给员工的慰问信
2019/06/24 职场文书
Java完整实现记事本代码
2022/06/16 Java/Android
详解apache编译安装httpd-2.4.54及三种风格的init程序特点和区别
2022/07/15 Servers