[Python web crawler and information extraction] .MOOC Beijing Institute of Technology this video. Taught me to crawl static single image, I use a Mac, so there's a change made in the form of an address file, the code is as follows:
import requests
import os
url = "https://pic.feizl.com/upload/allimg/170614/1JZ0G23-4.jpg"
root = "/Users/leo/Desktop/lianxi/"
path = root+url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(url)
with open(path,'wb') as f:
f.write(r.content)
f.close()
print("文件保存成功")
else:
print("文件已存在")
except:
print("爬取失败")
Show crawling successful, the lianxi new desktop folder, memory correct picture. Note that under Mac file format should be/Users/leo/Desktop/lianxi/