Python crawler(一):urllib的三种下载网页方法
来源:互联网 发布:詹姆斯各项数据排名 编辑:程序博客网 时间:2024/05/24 02:13
原教程来源于imooc
链接地址:http://www.imooc.com/learn/563
urllib的三种下载网页方法
1、测试代码
# urlopen的参数可以是request对象和urlfrom urllib import requestimport http.cookiejarurl = 'http://www.baidu.com' # 指定url为百度首页print('第一种方法:')response1 = request.urlopen(url) # 用urllib.request的urlopen方法,以url作为参数下载网页print(response1.getcode())print(len(response1.read()))print('第二种方法')req = request.Request(url) # 创建request对象添加特殊处理,url作为参数req.add_header('user-agent', 'Mozilla/5.0') # 把爬虫伪装成浏览器response2 = request.urlopen(req) # urlopen方法的参数改为requestprint(response2.getcode())print(len(response2.read()))print('第三种方法')cj = http.cookiejar.CookieJar() # 创建cookie容器opener = request.build_opener(request.HTTPCookieProcessor(cj)) # 创建opener,以cj为容器request.install_opener(opener) # 为request安装opener,使request具有cookie处理能力response3 = request.urlopen(url)print(response3.getcode())print(cj) # 打印cookie容器内容print(response3.read())
2、测试结果
0 0
- Python crawler(一):urllib的三种下载网页方法
- [python][urllib] 下载网页上的图片
- Python中的urllib,urllib三种不同的请求方式
- Python 3.4 - urllib.request 学习爬虫爬网页(一)
- 下载网页的三种方法
- urlib2下载网页的三种方法
- Python 下载网页的几种方法
- Python 下载网页的几种方法
- 【Python】Python的urllib模块、urllib2模块进行网页下载文件
- 【Python】Python的urllib模块、urllib2模块进行网页下载文件
- python使用re, os, httplib, urllib批量下载网页上的图片
- python 下载文件的三种方法
- python下载文件的三种方法
- python下载文件的三种方法
- Python Requests和urllib两种方式下载网页到本地
- Python爬虫(三)Urllib库的高级用法
- urlib2下载器网页的三种方法
- urllib2下载器网页的三种方法
- 浅谈 SQL 中的锁(六)查看锁的基本方法
- 小生初来CSDN
- AngularJs 利用百度地图API 定位当前位置 获取地址信息
- 关于etc目录
- MDN-闭包
- Python crawler(一):urllib的三种下载网页方法
- 欢迎使用CSDN-markdown编辑器
- 基于LVM快照mysql备份
- 1-DOM对象模型+事件(+note)
- 关于android新手引导的开源使用
- spring mvc 绑定对象 并进行校验
- 2016年度总结
- 很久很久前初学Java时的笔记--集合操作2
- git 退回操作