Python(16)_爬去百度圖片(urlopen和urlretrieve)
阿新 • • 發佈:2018-12-26
pen mage com span style col with jpg bad
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‘)
運行結果:
Python(16)_爬去百度圖片(urlopen和urlretrieve)