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

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

 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 response3.getcode()26     print "cookie为:"+str(cj)27     print response3.read()

执行结果

技术分享

 

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