Python 2.7 urllib2 cookielib 学习

# -*- coding:utf-8 -*-
import urllib2
import cookielib
url = 'http://wwww.baidu.com'
print "方法1"
response1 = urllib2.urlopen(url)
print response1.getcode()
print len(response1.read())

print "方法2"
request = urllib2.Request(url)
request.add_header('user-agent', 'Mozilla/5.0')
response2 = urllib2.urlopen(request)
print response2.getcode()
print len(response2.read())

print "方法3"
cj = cookielib.CookieJar()
ck_process = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(ck_process)
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
print response3.getcode()
print cj

 

posted on 2017-06-10 15:58  feel628  阅读(187)  评论(0)    收藏  举报

导航