- urllib2模块只能在python2.7中使用,它在python3中被拆分成urllib.request和urllib.error
- 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()
网友评论