python爬取今日的bing壁纸

#!/usr/bin/python
#你要的文件夹是   E://pics//    ps:没有的话帮你建一个
import requests
import os
from datetime import datetime
url="https://area.sinaapp.com/bingImg"
root="E://pics//"   #根目录
Time=datetime.today().strftime("%Y-%m-%d")
path=root+url.split('/')[-1]+Time+'.jpg' #根目录加上url中以反斜杠分割的最后一部分加上时间
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("爬取失败")


原文地址:https://www.cnblogs.com/yhm138/p/13371602.html