美文网首页
【python2.7】urllib2抓取网页基础

【python2.7】urllib2抓取网页基础

作者: tonyemail_st | 来源:发表于2017-11-02 21:12 被阅读0次

    参考官方文档:https://docs.python.org/2/library/urllib2.html

    1. urllib2模块只能在python2.7中使用,它在python3中被拆分成urllib.request和urllib.error
    2. urllib2模块定义的方法与类主要涉及:
    • basic and digest authentication
    • redirections
    • cookies
      These are provided by objects called handlers and openers.

    写法1

    import urllib2
    
    response = urllib2.urlopen("http://www.baidu.com")
    #print response.read()
    file = open("baidu.html", 'w')
    file.write(response.read())
    file.close()
    
    

    写法2

    import  urllib2
    req = urllib2.request("http://www.baidu.com")
    fp = urllib2.urlopen(req)
    file = open("baidu.html", 'w')
    file.write(fp.read())
    file.close()
    

    相关文章

      网友评论

          本文标题:【python2.7】urllib2抓取网页基础

          本文链接:https://www.haomeiwen.com/subject/sgiapxtx.html