本帖最后由 南归不NG 于 2021-1-23 07:51 编辑
编写环境:
python 3.8.3
requests 2.24.0
import requests
import re
import time
import random
import winreg
import os
''' 获取手机壁纸(高清) '''
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
}
def get_directory(): #判断路径是否存在,否的话生成
global path
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
path = winreg.QueryValueEx(key, "Desktop")[0]+"//pic"
print(path)
if not os.path.exists(path):
os.mkdir(path)
def getnum(i): #获取待下载壁纸id
global result
url = r"http://ser***************ical?limit=30&skip={}&adult=false&first=0&order=new".format(i*30)
response = requests.get(url=url,headers=headers).text
print('获取第{}个界面'.format(i+1))
time.sleep(1)
result = re.findall(r'"id": "(.*?)"',response)
return result
def getimg(m): #下载壁纸
for url in result:
time.sleep(random.random())
final_url = r"http://i***************k.com/{}?adult=false".format(url)
img = requests.get(url=final_url,headers=headers)
with open(path+'/{}.jpg'.format(m),'wb') as f:
f.write(img.content)
m+=1
get_directory()
for i in range(0,170): #爬取170页
m = i*30
getnum(i)
getimg(m)
可执行文件运行截图(运行时请勿关闭cmd,运行后会在桌面生成一个pic文件夹)
部分成果,若侵权请联系删除
因为水平不够,暂时没用多线程,所以速度较慢,将之前获取到的壁纸放到网盘中,供大家下载
链接: https://pan.baidu.com/s/1fbNUMaWH_2SZozev66ax6Q 提取码: a8gu 复制这段内容后打开百度网盘手机App,操作更方便哦
第一次发帖,虚心接受大家指教 |