zoukankan      html  css  js  c++  java
  • 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()

    执行结果

  • 相关阅读:
    Spring-Cloud之Feign
    Spring-Cloud之Ribbon原理剖析
    Spring-Cloud之Ribbon
    Spring-Cloud之Eureka
    显示列表
    顶点缓存对象(VBO)
    OpenGL观察轴
    OpenGL顶点数组
    OpenGL投影矩阵
    OpenGL变换
  • 原文地址:https://www.cnblogs.com/Mr-Cxy/p/5902447.html
Copyright © 2011-2022 走看看