zoukankan      html  css  js  c++  java
  • urllib的操作与使用--1

    #################################################
    '''
    版本:python2.7
    编辑器:pycharm
    标准库:urllib
    header网页头部信息:
    server:centos、microsoft-IIs
    content-type:text/html;charset=GBK
    last-modified:更新信息


    '''
    import urllib
    #查看urllib中所拥有的方法及变量
    # print dir(urllib)
    # #利用help来查看urllib帮助文档
    print help(urllib)
    print help(urllib.urlopen)
    url ="http://www.iplaypython.com/"
    html = urllib.urlopen(url)
    print html.read()
    print html.info()


    '''
    Date: Fri, 01 Sep 2017 16:16:50 GMT      #美国地区现在时间
    Server: Apache                           #linux系统下的apache服务器
    Last-Modified: Mon, 28 Aug 2017 02:05:22 GMT    #最进更新时间
    ETag: "52316-112e9-557c6ba29dc80"   #etag是表示搜索引擎优化,搜索引擎也是根据这个标签与上面一个标签来判断你的搜索引擎是否更新
    Accept-Ranges: bytes
    Content-Length: 70377
    Vary: Accept-Encoding
    Connection: close
    Content-Type: text/html
    '''


    #获取网页的状态码
    print html.getcode()
    #玩蛇王是utf-8的编码,所以就不用进行转编码
    #测试一个不是utf-8的网站
    url1 = "http://www.163.com/"
    html1 = urllib.urlopen(url1)
    # print html1.read().decode('gbk').encode('utf-8')
    #查看163.com的头文件信息
    # print html1.info()


    '''
    结果如下:
    Expires: Fri, 01 Sep 2017 16:12:59 GMT
    Date: Fri, 01 Sep 2017 16:11:39 GMT              #服务器信息所在地区的现在的时间
    Server: nginx                                    #服务器类型(linux或者是windows等)
    Content-Type: text/html; charset=GBK             #网页编码
    Vary: Accept-Encoding,User-Agent,Accept
    Cache-Control: max-age=80
    X-Via: 1.1 shq150:6 (Cdn Cache Server V2.0), 1.1 dunyidong72:1 (Cdn Cache Server V2.0)
    Connection: close
    '''


    #通过获取该网页的状态码来判断该网页是否可以访问,如果输出结果为200则可以访问,出现其他的则不可以访问。
    print html1.getcode()
    #获取网址
    print html1.geturl()


    '''
    普及网页的状态码的内容:
    200:表示可以正常访问
    301:重定向,自己去百度。
    404:网页不存在#可以随便创造一个网址来进行判断
    403:禁止访问:权限问题/或者是禁止爬虫抓取
    500:服务器忙碌
    <http权威指南,专门介绍http协议>
    web开发,这本书是必备的
    '''


    #网页抓取完成后,下载网页
    urllib.urlretrieve(url,'E:\abc.txt')#也可保存一个html文件。
    html.close()
    html1.close()


    ########################################################################################
    #1 decode()方法之ignore
    # import urllib
    # url = "http://www.163.com/"
    # html = urllib.urlopen(url)
    # content = html.read().decode("gbk",'ignore').encode("utf-8")
    # print content



    # 2 条件判断语句,自动化处理抓取的结果
    import urllib
    url = "http://www.iplaypython.com/"

    html=urllib.urlopen(url)
    print html.read()
    #相当于:html = urllib.urlopen(url).read()
    #Pprint html

    # print html.getcode()
    #相当于:html = urllib.urlopen(url).getcode()
    #print html

    # print html.geturl()
    # print html.info()

    code = html.getcode()
    if code ==200:
        print "网页正常"
        print html.read()
        print html.info()
    else:
        print "网页有问题"


    ###############################################################
    import urllib
    url = "http://www.iplaypython.com"
    info = urllib.urlopen(url).info()
    print info
    '''
    执行结果为:
    Date: Sat, 02 Sep 2017 05:08:27 GMT
    Server: Apache
    Last-Modified: Mon, 28 Aug 2017 02:05:22 GMT
    ETag: "52316-112e9-557c6ba29dc80"
    Accept-Ranges: bytes
    Content-Length: 70377
    Vary: Accept-Encoding
    Connection: close
    Content-Type: text/html
    '''

    print info.getparam("charset")
    #返回结果为:None   从中可知,有些网站时没有声明头部信息。



    #我们修改一个网址来执行
    url1="http://www.163.com"
    info1=urllib.urlopen(url1).info()
    print info1
    '''
    在这里为什么要输入一个字符串的参数呢?
    其返回值是在哪里获取的(头文件的内容类型中获取的)
    Expires: Sat, 02 Sep 2017 05:09:47 GMT
    Date: Sat, 02 Sep 2017 05:08:27 GMT
    Server: nginx
    Content-Type: text/html; charset=GBK
    Vary: Accept-Encoding,User-Agent,Accept
    Cache-Control: max-age=80
    X-Via: 1.1 shq153:8 (Cdn Cache Server V2.0), 1.1 dunyidong75:4 (Cdn Cache Server V2.0)
    Connection: close
    我们从头文件信息中可知:其内容类型中有charset=GBK,
    '''
    print info1.getparam("charset")
    #返回结果为:GBK

  • 相关阅读:
    [BZOJ3202][SDOI2013]项链
    Educational Codeforces Round 50
    [agc23E]Inversions
    [CF1016G]Appropriate Team
    [CF765F]Souvenirs
    [Luogu3733][HAOI2017]八纵八横
    [Luogu4609][FJOI2016]建筑师
    [BZOJ2159]Crash 的文明世界
    【学习笔记】Nim积
    PKUWC2020游记
  • 原文地址:https://www.cnblogs.com/papapython/p/7466564.html
Copyright © 2011-2022 走看看