Python-Learn/getBingWallpaper/getBingWallpaper.py
2021-11-22 11:22:33 +08:00

39 lines
927 B
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from lxml import etree
import datetime
import time
def main():
# 请求主页面
url = "https://cn.bing.com"
rs = requests.get(url=url)
# 对网页解析
try :
html = etree.HTML(rs.content)
imgUrl = html.xpath('//*[@property="og:image"]/@content')[0] + 'LaDigue_1920x1080.jpg'
print(imgUrl)
#imgUrl = 'https://bing.ioliu.cn/?p={}'.format(0)
except :
print('Get Url Error')
imgUrl = ''
# 获取图片内容
imgRs = requests.get(url=imgUrl)
time = datetime.datetime.now()
str_time = datetime.datetime.strftime(time, "%Y-%m-%d")
print(time)
path = "./bgImg/{}.jpg".format(str_time)
with open(path, "wb") as f:
f.write(imgRs.content)
if __name__ == '__main__':
while 1:
main()
time.sleep(24 * 60 * 60)