Python(16)_爬去百度图片(urlopen和urlretrieve)
import urllib.request image_url = 'http://img18.3lian.com/d/file/201709/21/f498e01633b5b704ebfe0385f52bad20.jpg' response = urllib.request.urlopen(url=image_url) # 二进制的形式保存,方法一 with open('qing.jpg','wb') as fp: fp.write(response.read())
方法2: 直接保存
import urllib.request image_url = 'http://img18.3lian.com/d/file/201709/21/f498e01633b5b704ebfe0385f52bad20.jpg' # urlretrieve 直接写入文件中,爬去的图片会保存在当前目录下 urllib.request.urlretrieve(image_url,'chun.jpg')
运行结果: