首页 > 代码库 > Python 2.7获取网站源代码的几种方式

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

 1 #coding:utf-8 2 import urllib2,cookielib 3  4 if __name__ == __main__: 5     root_url=https://www.baidu.com/ 6     # 第一种 7     print "第一种" 8     response1=urllib2.urlopen(root_url) 9     print response1.getcode()10     print len(response1.read())11 12     #第二种13     print "第二种"14     request=urllib2.Request(root_url)15     response2=urllib2.urlopen(request)16     print response2.getcode()17     print len(response2.read())18 19     # 第三种20     print "第三种"21     cj=cookielib.CookieJar()22     opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))23     urllib2.install_opener(opener)24     response3 = urllib2.urlopen(root_url)25     print cj,response3.getcode(),response3.read()

 

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