|
#coding:utf-8
#-------------------------------------------------------------------------------
# Name: 模块1
# Purpose:
#
# Author: mrwang
#
# Created: 18/04/2014
# Copyright: (c) mrwang 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
import urllib
def main():
url = 'http://xxxxxxx.xx'
html = urllib.urlopen(url)
# print html.read() #读取内容
# print html.read().decode('gbk').encode('utf-8') #乱码解决
# print html.read().decode('gbk', 'ignore').encode('utf-8') #一个页面多个编码 加ignore 忽略无法显示的字符
# print html.info() #查看网页头部信息
'''
Connection: close
Date: Fri, 18 Apr 2014 03:13:46 GMT
Server: Microsoft-IIS/6.0
MicrosoftOfficeWebServer: 5.0_Pub
pragma: no-cache
cache-control: private
Content-Length: 50853
Content-Type: text/html
Expires: Thu, 17 Apr 2014 03:13:44 GMT
Set-Cookie: web%5Fid=9952508807; path=/
Set-Cookie: ASPSESSIONIDQCTQRBQA=NJFIJEBAIFPPLGFKELICDDEL; path=/
Cache-control: no-cache
'''
# print html.getcode() #返回访问状态码
# print html.geturl() #返回网页
# urllib.urlretrieve(url, "c:\\abc.txt") #下载网页
# html.close() #关闭连接
'''
urllib.urlretrieve 方法使用
1 传入网址
2 传入本地保存路径文件名
3 一个函数调用,我们可以任意定义这个函数,但是这个函数一定要有三个参数
参数1 到目前为止传递的数据块数量
参数2 每个数据块的大小,单位byte,字节
参数3 获取的文件的大小 有时候会返回-1
'''
urllib.urlretrieve(url, 'C://a.html', callback)
def callback(a, b, c):
'''
@参数a 到目前为止传递的数据块数量
@参数b 每个数据块的大小,单位byte,字节
@参数c 获取的文件的大小 有时候会返回-1
'''
down_progress = 100.0 * a * b / c
if down_progress > 100:
down_progress = 100
print "%.2f%%" % down_progress, #后面加上 , 就不会换行
'''
0.00% 16.11% 32.22% 48.33% 64.44% 80.55% 96.66% 100.00%
'''
if __name__ == '__main__':
main()
|
|