2017-11-05 899
#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)
callback(a, b, c):
@参数a 到目前为止传递的数据块数量
@参数b 每个数据块的大小,单位byte,字节
@参数c 获取的文件的大小 有时候会返回-1
down_progress
100.0
*
a
b
/
c
if
down_progress >
100
:
print
"%.2f%%"
%
down_progress,
#后面加上 , 就不会换行
0.00% 16.11% 32.22% 48.33% 64.44% 80.55% 96.66% 100.00%
__name__
'__main__'
main()
本文转自 拖鞋崽 51CTO博客,原文链接:http://blog.51cto.com/1992mrwang/1398011
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。