运用python获取bing每日壁纸

运用python获取bing每日壁纸

测试环境 –> windows10,android(termux) : python3.5以上

首先需要安装python,下最新的就行

官方下载

确保pip也安装,接着安装相关依赖

  1. feedparser
  2. beautifulsoup4
  3. requests
1
2
安装以上依赖
pip install xxxx

脚本作用

执行脚本,会生成一个当前日期的文件夹,里面有当天的图片与json文件

源码如下

使用rsshub.app 每日壁纸 https://docs.rsshub.app/picture.html#bing-bi-zhi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-

import feedparser
from bs4 import BeautifulSoup
import json
import time
import os
import requests

# 获取图片信息
def getImg(info):
html = BeautifulSoup(info, 'html.parser')
img = html.img.attrs['src']
index = img.index('&')
img = img[:index]
return img

# 获取指定信息
def getInfo(url):
# 获取每日bing壁纸
d = feedparser.parse(url)
feed = d.feed
data = dict(title=feed.title,url=feed.link,desc=feed.description)
items = []
values = d.entries
for value in values:
item = dict()
item['desc'] = value.title
item['link'] = getImg(value.summary)
items.append(item)
data['values'] = items
return data

def main():
print('开始获取每日bing图片...')
url = 'https://rsshub.app/bing'
data = getInfo(url)
date = time.strftime('%Y-%m-%d')
ext = '.json'
file = date + ext

# 判断文件是否存在
if os.path.exists(date):
print('数据已存在')
return

# 首先创建文件夹
os.mkdir(date)
# 改变工作目录
os.chdir(date)

# 保存json文件
with open(file, 'w+', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)

# 保存图片
n = 0
for item in data['values']:
n = n + 1
link = item['link']
ext = os.path.splitext(link)[1]
img = requests.get(link)
file = str(n)+ext
with open(file, 'wb+') as f:
f.write(img.content)

print('结束获取每日bing图片')

if __name__ == '__main__':
main()