本帖最后由 姜蕴 于 2019-3-9 11:25 编辑
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'jiangwenwen'
from bs4 import BeautifulSoup
import requests
import time
import os
# 获取图片地址
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)
|