python下载网页的例子,代码如下:
import urllib2
response = urllib2.urlopen(‘http://www.baidu.com/’)
html = response.read()
fileHandle = open ( ‘test.html’, ‘w’ )
fileHandle.write ( html )
fileHandle.close()
print(‘已经完成任务!’)
说明:稍微改下这个代码,也可以下载图片:
import urllib2
url = “http://www.mianfeidianhua.net/wp-content/uploads/2015/02/1.gif”
response = urllib2.urlopen(url)
html = response.read()
wenjian = open (‘3.jpg’,’wb’)
wenjian.write (html)
wenjian.close()
print(‘已经完成任务!’)
wenjian = open (’3.jpg’,’wb’),其中‘wb’改成’w’则下载下来的图片是不清晰的。原因未知,以后补充。