python之下载每日必应壁纸

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'jiangwenwen'

from bs4 import BeautifulSoup
import requests
import time
import os
import wallpaper

# 获取图片地址
content = requests.get("https://cn.bing.com/?mkt=zh-CN").text
soup = BeautifulSoup(content, "lxml")
img_url = "https://cn.bing.com/" + soup.find(id="bgLink")["href"]
print(img_url)
str_time = time.strftime("%Y-%m-%d")
month = time.strftime("%m")

# 下载图片并保存到指定位置
img = requests.get(img_url)
img_path = r"H:\bing\%s" % (month)
if img.status_code == 200:
    # 如果文件夹不存在则创建
    folder = os.path.exists(img_path)
    if not folder:
        os.mkdir(img_path)
    open(r"H:\bing\%s\%s.jpg" % (month, str_time), 'wb').write(img.content)
    # 设置壁纸
    wallpaper.set_wallpaper(r"H:\bing\%s\%s.jpg" % (month, str_time))


posted @ 2019-03-07 01:24  姜文文  阅读(540)  评论(0编辑  收藏  举报