码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-09-24 01:53:40      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

 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获取网站源代码的几种方式

标签:

原文地址:http://www.cnblogs.com/Mr-Cxy/p/5902252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!