python2 urllib 笔记
python2 urllib 笔记
import urllib base='http://httpbin.org/'
ip=base+'ip'
r=urllib.urlopen(ip)
print r.geturl()
print r.read() #get
get=base+"get"
parms=urllib.urlencode({"name":"tom","age":18})
r=urllib.urlopen("%s?%s"%(get,parms))
print r.geturl()
print r.read() #post
post=base+"post"
parms=urllib.urlencode({"name":"tom","age":18})
r=urllib.urlopen(post,parms)
print r.geturl()
print r.read() #代理请求
proxies = {'http': 'http://proxy.example.com:8080/'}
opener = urllib.FancyURLopener(proxies)
f = opener.open("http://www.python.org")
f.read() #下载网页数据
#urllib.urlretrieve()
文件和网页下载
'''
Created on 2014年9月18日 @author: cocoajin 文件下载程序 ''' import urllib
import urlparse qihu360='http://dl.360safe.com/mac/safe/360InternetSecurity_1.0.75.dmg'
gitRF='http://gitref.org/zh/index.html' url=qihu360 #截取文件名,并设置保存路径为桌面
desk='/Users/teso/Desktop/'
up=urlparse.urlsplit(url)
fname=up.path.split('/')[-1]
path=desk+fname #下载回调
def showDN(dataNums,oneData,totalData):
'''
在下载过程之中的回调函数,回调下载的进度
dataNums:已下载的数据块
oneData:一个数据块的大小
totalData:总共的数据量
'''
download=100.0*dataNums*oneData/totalData
if download >= 100:
download=100.0
print 'download finished' print 'downloading %.2f%% ' % (download) re=urllib.urlretrieve(url, path,showDN)
print re
python2 urllib 笔记的更多相关文章
- python2 httplib 笔记
python2 httplib 笔记 #coding=utf-8 ''' Created on 2014年9月25日 @author: cocoajin ''' import httplib,url ...
- Effective Python2 读书笔记1
Item 2: Follow the PEP 8 Style Guide Naming Naming functions, variables, attributes lowercase_unders ...
- 回味Python2.7——笔记4
一.Python 标准库概览 1.操作系统接口 os 模块提供了很多与操作系统交互的函数: >>> import os >>> os.getcwd() # Retu ...
- Python3 urllib 与 Python2 urllib的变化
Infi-chu: http://www.cnblogs.com/Infi-chu/ Py2.x: Urllib库 Urllin2库 Py3.x: Urllib库 变化: 在Pytho2.x中使用im ...
- urllib笔记
在Python 3中,urllib2被合并到了urllib中,叫做urllib.request 和 urllib.error .urllib整个模块分为urllib.request, urllib.p ...
- Effective Python2 读书笔记3
Item 22: Prefer Helper Classes Over Bookkeeping with Dictionaries and Tuples For example, say you wa ...
- Effective Python2 读书笔记2
Item 14: Prefer Exceptions to Returning None Functions that returns None to indicate special meaning ...
- 回味Python2.7——笔记3
一.错误和异常 1.异常处理 >>> while True: ... try: ... x = int(raw_input("Please enter a number: ...
- 回味Python2.7——笔记2
一.模块 模块是包括 Python 定义和声明的文件.文件名就是模块名加上 .py 后缀.模块的模块名(做为一个字符串)可以由全局变量 __name__ 得到. 1. 模块可以导入其他的模块. 一个( ...
随机推荐
- ContentProvider官方教程(8)自定义MIME
MIME Type Reference Content providers can return standard MIME media types, or custom MIME type stri ...
- [Codeforces626F] Group Projects (DP)
Group Projects Description There are n students in a class working on group projects. The students w ...
- Swift 动画学习笔记
视频地址: http://www.swiftv.cn/course/i275v5lz 1,动画属性 position(位置),opacity(透明度,0 全透明,1 不透明),Scale(尺寸),Co ...
- Python 字符串、元组、字典转换成列表
- BZOJ 1042 硬币购物(完全背包+DP)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1042 题意:给出四种面值的硬币c1,c2,c3,c4.n个询问.每次询问用d1.d2.d ...
- ZOJ 3819 Average Score(平均分)
Description 题目描述 Bob is a freshman in Marjar University. He is clever and diligent. However, he is n ...
- monkeyrunner自动登录脚本
自己写了个平时测试的app的自动登录脚本,亲测可运行.读者参照时只需要改包名.activity名称.坐标值.账号和密码即可 查看坐标是多少的方法:使用手机的指针位置来实现:系统设置---开发者选项-- ...
- js判断ie11浏览器
var isIE11 = (/Trident\/7\./).test(navigator.userAgent);
- JMS【三】--ActiveMQ简单的HelloWorld实例
第一篇博文JMS[一]--JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文JMS[二 ...
- Nginx入门笔记之————配置文件结构
在nginx.conf的注释符号位# nginx文件的结构,这个对刚入门的同学,可以多看两眼. 默认的config: #user nobody; worker_processes ; #error_l ...