#coding:utf-8
import urllib2,cookielib if __name__ == '__main__':
root_url='https://www.baidu.com/'
# 第一种
print "第一种"
response1=urllib2.urlopen(root_url)
print response1.getcode()
print len(response1.read()) #第二种
print "第二种"
request=urllib2.Request(root_url)
response2=urllib2.urlopen(request)
print response2.getcode()
print len(response2.read()) # 第三种
print "第三种"
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(root_url)
print response3.getcode()
print "cookie为:"+str(cj)
print response3.read()

执行结果

Python 2.7获取网站源代码的几种方式_20160924-LMLPHP

05-11 21:54