1. 程式人生 > >python 圖片基礎爬取框架

python 圖片基礎爬取框架

import requests
import os
url = "http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg"
root = "D://pics//"
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("爬取失敗")