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实现通过代理服务器访问远程url的方法
Apr 29 Python
windows下python连接oracle数据库
Jun 07 Python
使用实现pandas读取csv文件指定的前几行
Apr 20 Python
python+splinter自动刷新抢票功能
Sep 25 Python
解决pyinstaller打包exe文件出现命令窗口一闪而过的问题
Oct 31 Python
Python 实现交换矩阵的行示例
Jun 26 Python
python logging添加filter教程
Dec 24 Python
python实现手势识别的示例(入门)
Apr 15 Python
Python xlrd模块导入过程及常用操作
Jun 10 Python
解决python中0x80072ee2错误的方法
Jul 19 Python
pandas取dataframe特定行列的实现方法
May 24 Python
分析Python感知线程状态的解决方案之Event与信号量
Jun 16 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
关于使用coreseek并为其做分页的介绍
2013/06/21 PHP
PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享
2014/09/27 PHP
PHP实现的AES双向加密解密功能示例【128位】
2018/09/03 PHP
PHP使用gearman进行异步的邮件或短信发送操作详解
2020/02/27 PHP
用js实现上传图片前的预览(TX的面试题)
2007/08/14 Javascript
获取HTML DOM节点元素的方法的总结
2009/08/21 Javascript
轻量级 JS ToolTip提示效果
2010/07/20 Javascript
基于jQuery的公告无限循环滚动实现代码
2012/05/11 Javascript
js获取IFRAME当前的URL的方法
2013/11/13 Javascript
Javascript中prototype属性实现给内置对象添加新的方法
2015/05/14 Javascript
TypeScript入门-接口
2017/03/30 Javascript
javascript+html5+css3自定义提示窗口
2017/06/21 Javascript
javascript 通过键名获取键盘的keyCode方法
2017/12/31 Javascript
Vue.js分页组件实现:diVuePagination的使用详解
2018/01/10 Javascript
Element-UI踩坑之Pagination组件的使用
2018/10/29 Javascript
微信小程序rich-text富文本用法实例分析
2019/05/20 Javascript
[01:25]DOTA2超级联赛专访iG 将调整状态找回自己
2013/06/05 DOTA
python类和函数中使用静态变量的方法
2015/05/09 Python
Python实现迭代时使用索引的方法示例
2018/06/05 Python
python实现贪吃蛇游戏
2020/03/21 Python
python sort、sort_index方法代码实例
2019/03/28 Python
python图像和办公文档处理总结
2019/05/28 Python
Windows平台Python编程必会模块之pywin32介绍
2019/10/01 Python
tensorflow模型继续训练 fineturn实例
2020/01/21 Python
Xadmin+rules实现多选行权限方式(级联效果)
2020/04/07 Python
django创建超级用户时指定添加其它字段方式
2020/05/14 Python
查看keras各种网络结构各层的名字方式
2020/06/11 Python
Python通过队列来实现进程间通信的示例
2020/10/14 Python
局域网标准
2016/09/10 面试题
JPA的优势都有哪些
2013/07/04 面试题
动漫设计与制作专业推荐信
2014/07/07 职场文书
拿破仑传读书笔记
2015/07/01 职场文书
任命书格式模板
2015/09/22 职场文书
银行柜员优质服务心得体会
2016/01/22 职场文书
Python的flask接收前台的ajax的post数据和get数据的方法
2021/04/12 Python
pytorch 预训练模型读取修改相关参数的填坑问题
2021/06/05 Python