每天一张背景图
效果图
每天跑跑代码
还是原图哦
手把手教你利用爬虫每天更换壁纸
设置背景图
源代码
try:
from requests_html import HTMLSession
import requests
import time
import os
except:
import os
os.system('sudo pip3 install requests_html')
from requests_html import HTMLSession
import requests
import time
import os
session = HTMLSession()
madir = 'C:/Users/Administrator/Desktop/背景' # 图片保存地址
Turnpage = 0 # 总页数
page = 1 # 当前爬取的页数
def contrast(title):
file = os.listdir(madir)
if file != []:
if title in file:
return False
else:
return True
else:
return True
def get_girl_list(dig):
if not os.path.exists(madir):
os.mkdir(madir)
url = 'https://bing.ioliu.cn/?p=%s' % dig
r = session.get(url)
about = r.html.find('div.progressive')
global Turnpage
Turnpage = int(r.html.find('div.page>span', first=True).text.split('/')[1])
for item in about:
img = item.find('img', first=True)
sl = img.attrs['src'].split('_640x480')[0]
name = sl.split('/')[-1]
title = name + '.jpg'
print(contrast(title))
if contrast(title):
get_girl_detail(name)
global page
page += 1
if page < Turnpage:
Turn_the_page(page)
def get_girl_detail(name):
time.sleep(0.3) # 毫秒级时间戳
title = name + '.jpg'
# f = open('C:/Users/Administrator/Desktop/测试/%s.jpg' % title, 'ab')
img_response = requests.get('https://cn.bing.com/th?id=OHR.' + name + '_UHD.jpg')
with open(madir + '/%s' % title, 'wb') as f:
f.write(img_response.content)
print(title + '下载完毕')
def Turn_the_page(page):
print('这是第%s页' % page)
get_girl_list(page)
Turn_the_page(page)
# http://h2.ioliu.cn/bing/EDCScotland_ZH-CN2038527689_1920x1080.jpg
# https://bing.ioliu.cn/photo/EDCScotland_ZH-CN2038527689