#!/usr/bin/python
# encoding=utf-8
import requests
from bs4 import BeautifulSoup
class bizhi:
def getheaders(self):
return {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Host": "www.bizhi360.com",
"Referer": "http://www.bizhi360.com/meinv/",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36",
}
def getHtml(self,url):
r = requests.get(url,headers=self.getheaders())
result = r.content.decode('utf-8')
html = BeautifulSoup(result,'html.parser')
ul = html.find('ul')
all_li = ul.find_all('li')
for i in all_li:
a = "http://www.bizhi360.com"+i.find('a').get('href')
# 详情页
details_con = requests.get(a,headers=self.getheaders())
details_html = BeautifulSoup(details_con.content.decode('utf-8'),'html.parser')
dul = details_html.find('div',class_='pic-list')
if dul:
all_dli = dul.find_all('li')
if all_dli:
#多图
for di in all_dli:
xurl = 'http://www.bizhi360.com'+di.find('a').get('href')
img_html = requests.get(xurl, headers=self.getheaders())
self.getImg(img_html)
else:
self.getImg(details_con)
def imgheader(self):
return {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Host": "pic.bizhi360.com",
"If-Modified-Since": "Wed, 01 Sep 2021 14:24:33 GMT",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36",
}
def getImg(self,img_html):
details_html = BeautifulSoup(img_html.content.decode('utf-8'), 'html.parser')
img_url = details_html.find('figure').find('a').get('href')
title = details_html.find('title').getText()
img_html = requests.get(img_url, headers=self.imgheader())
img_html_c = img_html.content
ext = img_url.split('.') # 获取后缀名
with open('./img/' + str(title) + "." + str(ext[3]), 'wb') as f:
f.write(img_html_c)
f.close()
if __name__ == '__main__':
x = bizhi()
# x.getHtml("http://www.bizhi360.com/meinv") #http://www.bizhi360.com/meinv/list_2.html
for i in range(2,100):
try:
x.getHtml(
"http://www.bizhi360.com/meinv/list_" + str(i) + ".html") # http://www.bizhi360.com/meinv/list_2.html
except Exception:
print('1')
定时切换壁纸代码
#!/usr/bin/python
# encoding=utf-8
import threading
import os
import random
import ctypes
def change_background():
# 注意文件夹路径
rootdir = 'c:/www/python/img'
for parent, dirnames, filenames in os.walk(rootdir):
file_names = filenames
x = random.randint(0, len(file_names)-1)
file_path = rootdir+"/"+filenames[x]
ctypes.windll.user32.SystemParametersInfoW(20, 0, file_path, 3)
def timer():
# 5分钟切换一次
t = threading.Timer(300,change_background)
t.start()
if __name__ == '__main__':
timer()