[Python] 抓取必应每日一图,设置为桌面壁纸

在Windows 10 64位系统环境下,使用 Python 3.6 进行图片抓取和设置壁纸操作。
其中,图片链接 https://area.sinaapp.com/bingImg/ 为 Bing 每日一图的地址,解析可以获得图片文件的地址,下载即可。之后使用 ctypes 工具进行壁纸设置。颇为简单。结合 AHK (auto hot key) 可以用快捷按键的方式运行脚本更新壁纸。颇为方便。 :)

SetBingImgAsWallpaper.py

"""
程序功能:抓取必应每日一图,设置为桌面壁纸
"""

import urllib.request
import requests         
import os.path
import ctypes

def save_img(img_url,dirname):
    #保存图片到磁盘文件夹dirname中
    try:
        if not os.path.exists(dirname):
            print ('文件夹',dirname,'不存在,重新建立')
            #os.mkdir(dirname)
            os.makedirs(dirname)
        #获得图片文件名,包括后缀
        basename = os.path.basename(img_url)
        #拼接目录与文件名,得到图片路径
        filepath = os.path.join(dirname, basename)
        #下载图片,并保存到文件夹中
        urllib.request.urlretrieve(img_url,filepath)
    except IOError as e:
        print ('文件操作失败',e)
    except Exception as e:
        print ('错误 :',e)
    print("Save", filepath, "successfully!")

    return filepath

# 请求网页,跳转到最终 img 地址
def get_img_url(raw_img_url = "https://area.sinaapp.com/bingImg/"):
    r = requests.get(raw_img_url)       
    img_url = r.url # 得到图片文件的网址
    print('img_url:', img_url)
    return img_url

# 设置图片绝对路径 filepath 所指向的图片为壁纸
def set_img_as_wallpaper(filepath):
    ctypes.windll.user32.SystemParametersInfoW(20, 0, filepath, 0)

def main():
    dirname = "E:\\Pictures\\bingImg"       # 图片要被保存在的位置
    img_url = get_img_url()
    filepath = save_img(img_url, dirname)   # 图片文件的的路径
    set_img_as_wallpaper(filepath)

main()

MyScript.ahk

;Ctrl + Alt + P 快捷键运行SetBingImgAsWallpaper.py脚本设置壁纸为Bing每日一图(需要在联网状态运行)
^!P::
Run python SetBingImgAsWallpaper.py
Return

抓取的图片

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要使用Python 3来批量下载Bing搜索的图片,可以按照以下步骤进行: 1. 首先,需要安装requests和beautifulsoup4这两个库。可以使用以下命令安装它们: ``` pip install requests pip install beautifulsoup4 ``` 2. 导入所需的模块: ```python import requests from bs4 import BeautifulSoup import os ``` 3. 创建一个函数来下载图片: ```python def download_image(url, save_dir): filename = os.path.join(save_dir, url.split('/')[-1]) response = requests.get(url, stream=True) if response.status_code == 200: with open(filename, 'wb') as file: file.write(response.content) print('成功下载图片:', filename) else: print('无法下载图片:', url) ``` 4. 定义一个函数来搜索并批量下载图片: ```python def download_images(query, save_dir, num_images): search_url = 'https://www.bing.com/images/search?q=' + query response = requests.get(search_url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') image_tags = soup.find_all('img') count = 0 for image_tag in image_tags: image_url = image_tag['src'] if image_url.startswith('https://'): download_image(image_url, save_dir) count += 1 if count == num_images: break else: print('搜索失败') ``` 5. 调用download_images函数并指定搜索的关键字、保存目录以及要下载的图片数量: ```python query = '美食' # 搜索关键字 save_dir = 'images' # 图片保存目录 num_images = 10 # 要下载的图片数量 download_images(query, save_dir, num_images) ``` 运行脚本后,将会在指定目录下下载指定数量的Bing搜索结果中的图片。 需要注意的是,根据Bing的使用条款,禁止使用非官方API进行批量下载图片。因此,在使用此方式之前,请确保你已阅读并遵守相关使用条款。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值