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. java交换两个数字位置

    第一种:在main输出,通过反射实现 1 public static void main(String[] args) throws Exception { Integer a = 1; Intege ...

  2. 精通D3.js学习笔记(2)比例尺和坐标

    1.线性比例尺 d3.scale.linear()   创建一个线性比例尺           .domain([0,500]) 定义域           .range([0,1000]) 值域 l ...

  3. 2016年11月9日 星期三 --出埃及记 Exodus 19:25

    2016年11月9日 星期三 --出埃及记 Exodus 19:25 So Moses went down to the people and told them.于是摩西下到百姓那里告诉他们.

  4. 电脑能上网,手机连上wifi不能上网

    电脑能上网,手机连上wifi不能上网  ,其实只要把手机的dhcp 改为我们熟悉的就行了 我此处就设置为114.114.114.114

  5. Intent官方教程(1)简介和作用

    Intents An Intent is a messaging object you can use to request an action from another app component. ...

  6. BZOJ 3489 A simple rmq problem(可持久化线段树)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3489 题意:一个数列.每次询问一个区间内出现一次的最大的数字是多少. 思路:设la ...

  7. Block很简单,就像delegate的简化版

    代理设计模式对于iOS开发的人来说肯定很熟悉了,代理delegate就是委托另一个对象来帮忙完成一件事情,为什么要委托别人来做呢,这其实是MVC设计模式中的模块分工问题,例如View对象它只负责显示界 ...

  8. Freebie - Utility Form: Generate Excel Report From SQL Query In Oracle Forms 6i And 11g

    Sharing a form to generate Excel file report from SQL query in Oracle Forms. This form can be used i ...

  9. Music Player团队项目(一)

    团队成员及分工 团队: Blue 团队共有六人 姓名:     学号后四位:       贡献分: 张   宇(队长)  1152          1+1.8=2.8分 侯贺琦          1 ...

  10. Linux链接库一(动态库,静态库,库放在什么路径下)

    http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http ...