首页 > 代码库 > python2 urllib 笔记

python2 urllib 笔记

python2 urllib 笔记

import urllibbase=http://httpbin.org/ip=base+ipr=urllib.urlopen(ip)print r.geturl()print r.read()#getget=base+"get"parms=urllib.urlencode({"name":"tom","age":18})r=urllib.urlopen("%s?%s"%(get,parms))print r.geturl()print r.read()#postpost=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 urllibimport urlparseqihu360=http://dl.360safe.com/mac/safe/360InternetSecurity_1.0.75.dmggitRF=http://gitref.org/zh/index.htmlurl=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 笔记