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 笔记的更多相关文章

  1. python2 httplib 笔记

    python2  httplib 笔记 #coding=utf-8 ''' Created on 2014年9月25日 @author: cocoajin ''' import httplib,url ...

  2. Effective Python2 读书笔记1

    Item 2: Follow the PEP 8 Style Guide Naming Naming functions, variables, attributes lowercase_unders ...

  3. 回味Python2.7——笔记4

    一.Python 标准库概览 1.操作系统接口 os 模块提供了很多与操作系统交互的函数: >>> import os >>> os.getcwd() # Retu ...

  4. Python3 urllib 与 Python2 urllib的变化

    Infi-chu: http://www.cnblogs.com/Infi-chu/ Py2.x: Urllib库 Urllin2库 Py3.x: Urllib库 变化: 在Pytho2.x中使用im ...

  5. urllib笔记

    在Python 3中,urllib2被合并到了urllib中,叫做urllib.request 和 urllib.error .urllib整个模块分为urllib.request, urllib.p ...

  6. Effective Python2 读书笔记3

    Item 22: Prefer Helper Classes Over Bookkeeping with Dictionaries and Tuples For example, say you wa ...

  7. Effective Python2 读书笔记2

    Item 14: Prefer Exceptions to Returning None Functions that returns None to indicate special meaning ...

  8. 回味Python2.7——笔记3

    一.错误和异常 1.异常处理 >>> while True: ... try: ... x = int(raw_input("Please enter a number: ...

  9. 回味Python2.7——笔记2

    一.模块 模块是包括 Python 定义和声明的文件.文件名就是模块名加上 .py 后缀.模块的模块名(做为一个字符串)可以由全局变量 __name__ 得到. 1. 模块可以导入其他的模块. 一个( ...

随机推荐

  1. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  2. easyui commbox嵌入一个checkbox的实现

    function InitComBoBox(datagrid, combxid, formid, url, valueField, textField,_prompt) { $(combxid).co ...

  3. P2679 子串

    http://www.luogu.org/problem/show?pid=2679 题目描述 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后 ...

  4. ecshop商品分类页获取相册列表方法

    第1步:找到根目录的category.php文件,查找约:486行左右(注意这不是准确位置,看实际的哦),找到这个函数: /** * 获得分类下的商品 * * @access public * @pa ...

  5. ocruntime

    原作: http://www.jianshu.com/p/25a319aee33d 三种方法的选择 Runtime提供三种方式来将原来的方法实现代替掉,那该怎样选择它们呢? Method Resolu ...

  6. 【Java】Map杂谈,hashcode()、equals()、HashMap、TreeMap、LinkedHashMap、ConcurrentHashMap

    参考的优秀文章: <Java编程思想>第四版 <Effective Java>第二版 Map接口是映射表的结构,维护键对象与值对象的对应关系,称键值对. > hashco ...

  7. CodeForces 651C Watchmen map

    Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...

  8. 如何通过Button获取UITableViewCell

    发现一个奇怪的问题: 手机(ios7) 2015-06-17 15:11:29.323 ***[1412:60b]  [btn superview] =  UITableViewCellContent ...

  9. swift + jj实践

    1,UIButton里面的字体和图片左对齐 button.imageEdgeInsets = UIEdgeInsetsMake(0,180/2,0.0,320/2) let btnRect = but ...

  10. 【Unity3D游戏开发】性能优化之spine提高80~90%的效率 (三一)

    Spine效率低 Unity项目加载spine动画,经常会出现卡顿的情况,如游戏中瞬间播放一个动画,打开一个带spine动画的界面.尤其是SkeletonRenderer.Awake时,会瞬间出现大量 ...