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

参考官方文档: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抓取网页基础)