- 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()