python urllib的用法实例
2019独角兽企业重金招聘Python工程师标准>>>
import urllib2
import urllib
import jsondef _post_json_by_urllib(url,jsondata):request=urllib2.Request(url,jsondata)request.add_header("Content-type","application/json")request.get_method=lambda :"POST"request=urllib2.urlopen(request)return request.read()class Api(object):def __init__(self,url):self.url=urldef get(self,data={}):#get请求assert type(data)==dictdata=dict([((key if type(key)!=unicode else key.encode("utf-8")) ,\(v if type(v)!=unicode else v.encode("utf-8")) ) for key,v in data.items()])querystring=urllib.urlencode(data)if "?" not in self.url:url=self.url+"?"+querystringelse:url=self.url+'&'+querystringtry:fd=urllib2.urlopen(url)data=fd.read()return (True,data)except Exception as e :return (False,str(e))def post(self,data={},is_json=False):#post请求assert type(data)==dictdata=dict([((key if type(key)!=unicode else key.encode("utf-8")) ,\(v if type(v)!=unicode else v.encode("utf-8")) ) for key,v in data.items()])if is_json==False:data=urllib.urlencode(data)try:#req = urllib2.Request(self.url,data)data=urllib2.urlopen(self.url,data,timeout=3)return (True,data.read())except Exception as e :return (False,str(e))else:try:data=json.dumps(data)result=_post_json_by_urllib(self.url,data)return (True,result)except Exception as e :return (False,str(e))
转载于:https://my.oschina.net/u/1458120/blog/666249
python urllib的用法实例相关推荐
- python装饰器实例-Python装饰器用法实例总结
本文实例讲述了Python装饰器用法.分享给大家供大家参考,具体如下: 一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能, ...
- Python回调函数用法实例
什么是回调函数? 我们绕点远路来回答这个问题. 编程分为两类:系统编程(system programming)和应用编程(application programming).所谓系统编程,简单来说,就是 ...
- python装饰器作用-Python装饰器用法实例总结
一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象.简单的说装饰器就是一个用来返回函数的函数 ...
- python shutil模块用法实例分析_Python shutil模块用法实例分析
分享大神指教Python中的shutil模块的rmtree()方法如分享大神指教Python中的shutil模块的rmtree()方法如何实现.思路是怎样的rmtree() 是用来删除文件目录及其中的 ...
- python shutil模块用法实例分析_python之shutil模块使用方法(三分钟了解)
文章目录 shutil模块 1.1简介 shutil模块提供了一些针对文件和目录,压缩包的高级操作,主要是拷贝.移动.对于单个文件的操作,可参考os模块的用法. 1.2 作用及作用范围 作用: 快速的 ...
- python中global用法实例
函数内定义的变量是局部变量,模块内定义的变量为全局变量.如果想在函数中定义全局作用域需要加上globa来修饰 global修饰的用法: 1.在模块层面定义的变量,是全局变量,在模块任意处都可以调用 a ...
- python argparse模块用法实例详解
转载自https://zhuanlan.zhihu.com/p/56922793 argsparse是python的命令行解析的标准模块,内置于python,不需要安装.这个库可以让我们直接在命令行中 ...
- python中messagebox用法实例_pyqt4教程之messagebox使用示例分享
#coding=utf-8 #对话框 import sys from PyQt4 import QtGui, QtCore class Window( QtGui.QWidget ): def __i ...
- python守护线程_Python守护线程用法实例
本文实例讲述了Python守护线程用法.分享给大家供大家参考,具体如下: 如果你设置一个线程为守护线程,就表示你在说这个线程是不重要的,在进程退出的时候,不用等待这个线程退出.如果你的主线程在退出的时 ...
最新文章
- 重做日志文件(redo log file)和归档日志文件(archive log file)
- 理解 Kotlin 中的属性(property)
- (二)网络安全概论之云安全
- 数据挖掘150道笔试题
- python- 基础 map方法
- C# 大数组赋值给小数组,小数组赋值给大数组
- Block Formatting Contexts(块级格式化上下文)
- MSSQL调优实战一 乱建聚集索引的后果
- 思达报表工具Style Report基础教程—用选择列表,选择树进行数据过滤
- H264之sps解析分辨率
- 手工画图和计算机画图的内在联系,浅谈计算机绘图有关的论文(2)
- 【图像去噪】基于matlab GUI均值+中值图像去噪【含Matlab源码 1815期】
- SAR信号处理之窗函数8——布莱克曼(Blackman)窗
- 哥德巴赫猜想python
- IC618的资源分享及IC618电路显示黄色问题
- python 新式类 旧式类_Python新式类与经典类(旧式类)的区别
- Arrays.copyOf()方法用法
- do while“直到”型循环
- 基于vuex的物业管理系统APP及网页后台设计
- android nfc扇区加密,nfc读取加密扇区