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. datagrid后台分页js.js

    $(function () { gridbind(); bindData(); }); //表格绑定function gridbind() { $('#dg').datagrid({ title: ' ...

  2. SqlSever基础 detalength函数 字节的个数

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  3. 数据库批量修改表名,增加前缀(SQL server)

    exec sp_msforeachtable @command1=' declare @o sysname,@n sysname select @o=''?'' ,@n=stuff(@o,1,7,'' ...

  4. HTML5媒体

    1.视频格式 格式 文件 描述 AVI .avi AVI (Audio Video Interleave) 格式是由微软开发的.所有运行 Windows 的计算机都支持 AVI 格式.它是因特网上很常 ...

  5. Cheatsheet: 2013 07.01 ~ 07.08

    .NET The overhead of async/await in NET 4.5 await Task, Task.Wait and Friends 350 Interview Question ...

  6. Date Picker Calendar For Oracle Forms 6i

    Giving date picker calendar option to user for date type fields in Oracle Forms. I am providing you ...

  7. Django的Many-to-Many(多对多)模型

      Django的Many-to-Many(多对多)模型 日期:2012-05-05 |  来源:未知 |  作者:redice |  人围观 |  1 人鼓掌了! 鲲鹏Web数据抓取 - 专业Web ...

  8. Python使用requirements.txt安装类库

    摘要:我们为何要应用requirements.txt呢? 首要应用目标: 任何 运用顺序 平常 须要设置装置 所需并 依附 一组类库去知足 事情请求 . 请求 文件 是 指定 战 一次性 装置 包 的 ...

  9. 【Protle99SE】PCB中各层的含义【小汇】

    忽然发现,对solder和paste,layer和plane这两对有些糊涂了,摘录网络中的文章如下: [http://www.360doc.com/content/10/0608/15/514342_ ...

  10. Linux配置apache等系列

    1.Linux下安装.配置PHP环境 2.ubuntu12.0.4安装apache, php ,mysql 3   CentOs中mysql的安装与配置