python_urllib2下载网页的三种方式
2016-03-29 19:16
639 查看
# -*- coding: utf-8 -*- import urllib2 import cookielib url="http://www.baidu.com" #define URL # three ways to capturing webs print "first way:" response1 = urllib2.urlopen(url) print response1.getcode()#return 200,the web can be accessible print len(response1.read())# return the length of this web print "second way:" request = urllib2.Request(url)#调用request对象 request.add_header("user-agent" , "Mozilla/5.0")#把爬虫伪装成一个浏览器,Mozilla/5.0是火狐客户端浏览器版本 response2 = urllib2.urlopen(request) print response2.getcode() print len(response2.read()) print "third way:" cj= cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) response3 = urllib2.urlopen(url) print response3.getcode() print cj #打印cookie内容 print response3.read()
相关文章推荐
- Python获取豆瓣新书列表
- Python学习笔记(六)错误、调试和测试
- python正则表达式(关于re)
- 【python】dict。字典
- python连接MySQL源码
- python tuple 操作
- Python 实现简单的爬虫功能并保存到本地
- python re模块汇总
- python sproto支持64位有符号整数
- python之 sqlalchemy
- php crypt in python
- python--爬虫入门(七)urllib库初体验以及中文编码问题的探讨
- python基础
- 枚举法解决推理问题
- Python 实现倒排索引
- python写一段脚本代码自动完成输入(目录下的所有)文件的数据替换(修改数据和替换数据都是输入的)【转】
- Python内嵌函数的访问方法
- python splinter 小坑说明
- A Byte of Python 笔记(4)控制流:if、for、while、break、continue
- Python之select模块解析