您的位置:首页 > 编程语言 > Python开发

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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: