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. Intent传递对象——Serializable和Parcelable区别

    为什么要将对象序列化? 1.永久性保存对象,保存对象的字节序列到本地文件中: 2.用过序列化对象在网络中传递对象: 3.通过序列化对象在进程间传递对象. 1.实现Serializable接口 Seri ...

  2. OpenLayers学习记录(1)

    1.部署自己的服务器 首先下载openlayers的源码.解压后里面有很多内容.我们只需要拷贝目录下的OpenLayer.js.根目录下的lib目录.根目录下的img目录 theme目录 到你网站的o ...

  3. C语言中'\0'与'\n'

    '\0'表示ASCII编号为0的字符,在C语言中最常用于代表字符串结束的标志.'\n'表示ASCII编号为13的字符,代表回车键,输出这个字符就会换一行. '\0'作为字符串的结束标志,本身会占用一个 ...

  4. afxmessagebox和messagebox

    MessageBox()是Win32API函数.后者是mfc中的全局函数.在MFC中能用MessageBox()的地方都能用AfxMessageBox(). afxmessagebox更多的时候是用于 ...

  5. 个人对js闭包的理解

      闭包算是前端面试的基础题,但我看了很多关于闭包的文章博客,但感觉很多对于闭包的理想还是有分歧的,现在网上对闭包的理解一般是两种: 有些文章认为闭包必须要返回嵌套函数中里面用到外面函数局部变量的方法 ...

  6. Pre-Query trigger in Oracle D2k / Oracle Forms

    Pre-Query trigger in Oracle D2k / Oracle Forms DescriptionFires during Execute Query or Count Query ...

  7. Using SSH on Linux

    This document covers the SSH client on the Linux Operating System and other OSes that use OpenSSH. W ...

  8. Codeforces Round #272 (Div. 2) C. Dreamoon and Sums 数学

    C. Dreamoon and Sums time limit per test 1.5 seconds memory limit per test 256 megabytes input stand ...

  9. XAF应用开发教程(七)外观控制模块

    很多时候,我们需要按照不同的条件显示不同的效果,在传统的软件开发中,我们会直接使用 控件名称.BackColor,Enable,Visible等属性进行控制. 如果一个业务对象在多处使用,要么我们会去 ...

  10. NPN&PNP

    一.晶体管基础知识 晶体管分2种:NPN.PNP 晶体管通常封装为TO-92,下面是元件实物图 和 元件符合: NPN: 当电压和电流被加到基极上时,NPN晶体管: 其工作原理: 就像水龙头—给控制开 ...