python爬虫学习小程序

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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  *  *  /  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()
本文转自    拖鞋崽      51CTO博客,原文链接:http://blog.51cto.com/1992mrwang/1398011
相关文章
|
1天前
|
Web App开发 测试技术 Python
【如何学习python自动化测试】—— 浏览器驱动的安装 以及 如何更新driver
【如何学习python自动化测试】—— 浏览器驱动的安装 以及 如何更新driver
6 0
|
1天前
|
测试技术 BI Python
【如何学习Python自动化测试】—— HTMLTestRunner 生成测试报告
【如何学习Python自动化测试】—— HTMLTestRunner 生成测试报告
6 0
|
1天前
|
Java 测试技术 数据库连接
【如何学习Python自动化测试】—— Python 的 unittest 框架
【如何学习Python自动化测试】—— Python 的 unittest 框架
4 0
|
1天前
|
存储 网络协议 测试技术
【如何学习Python自动化测试】—— Cookie 处理
【如何学习Python自动化测试】—— Cookie 处理
7 1
|
1天前
|
Web App开发 JavaScript 测试技术
【如何学习Python自动化测试】—— 警告框处理
【如何学习Python自动化测试】—— 警告框处理
6 0
|
1天前
|
测试技术 Python Windows
【如何学习Python自动化测试】—— 多层窗口定位
【如何学习Python自动化测试】—— 多层窗口定位
8 1
|
1天前
|
测试技术 数据安全/隐私保护 Python
【如何学习Python自动化测试】—— 鼠标键盘操作
【如何学习Python自动化测试】—— 鼠标键盘操作
4 0
|
1天前
|
编解码 测试技术 Python
【如何学习Python自动化测试】—— 浏览器操作
【如何学习Python自动化测试】—— 浏览器操作
3 0
|
1天前
|
JavaScript 测试技术 Python
【如何学习Python自动化测试】—— 时间等待
【如何学习Python自动化测试】—— 时间等待
4 1
|
1天前
|
Web App开发 前端开发 测试技术
【如何学习Python自动化测试】—— 页面元素定位
【如何学习Python自动化测试】—— 页面元素定位
4 1

热门文章

最新文章