标签:列表 决定 pat json input makedirs apple block 模糊
壁纸的选择其实很大程度上能看出电脑主人的内心世界,有的人喜欢风景,有的人喜欢星空,有的人喜欢美女,有的人喜欢动物。然而,终究有一天你已经产生审美疲劳了,但你下定决定要换壁纸的时候,又发现网上的壁纸要么分辨率低,要么带有水印。
项目源码
1 # -*- coding:utf-8 -*- 2 3 from requests import get 4 from filetype import guess 5 from os import rename 6 from os import makedirs 7 from os.path import exists 8 from json import loads 9 from contextlib import closing 10 11 12 # 文件下载器 13 def Down_load(file_url, file_full_name, now_photo_count, all_photo_count): 14 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"} 15 16 # 开始下载图片 17 with closing(get(file_url, headers=headers, stream=True)) as response: 18 chunk_size = 1024 # 单次请求最大值 19 content_size = int(response.headers[‘content-length‘]) # 文件总大小 20 data_count = 0 # 当前已传输的大小 21 with open(file_full_name, "wb") as file: 22 for data in response.iter_content(chunk_size=chunk_size): 23 file.write(data) 24 done_block = int((data_count / content_size) * 50) 25 data_count = data_count + len(data) 26 now_jd = (data_count / content_size) * 100 27 print("\r %s:[%s%s] %d%% %d/%d" % (file_full_name, done_block * ‘█‘, ‘ ‘ * (50 - 1 - done_block), now_jd, now_photo_count, all_photo_count), end=" ") 28 29 # 下载完图片后获取图片扩展名,并为其增加扩展名 30 file_type = guess(file_full_name) 31 rename(file_full_name, file_full_name + ‘.‘ + file_type.extension) 32 33 34 35 # 爬取不同类型图片 36 def crawler_photo(type_id, photo_count): 37 38 # 最新 1, 最热 2, 女生 3, 星空 4 39 if(type_id == 1): 40 url = ‘https://service.paper.meiyuan.in/api/v2/columns/flow/5c68ffb9463b7fbfe72b0db0?page=1&per_page=‘ + str(photo_count) 41 elif(type_id == 2): 42 url = ‘https://service.paper.meiyuan.in/api/v2/columns/flow/5c69251c9b1c011c41bb97be?page=1&per_page=‘ + str(photo_count) 43 elif(type_id == 3): 44 url = ‘https://service.paper.meiyuan.in/api/v2/columns/flow/5c81087e6aee28c541eefc26?page=1&per_page=‘ + str(photo_count) 45 elif(type_id == 4): 46 url = ‘https://service.paper.meiyuan.in/api/v2/columns/flow/5c81f64c96fad8fe211f5367?page=1&per_page=‘ + str(photo_count) 47 48 # 获取图片列表数据 49 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"} 50 respond = get(url, headers=headers) 51 photo_data = loads(respond.content) 52 53 # 已经下载的图片张数 54 now_photo_count = 1 55 56 # 所有图片张数 57 all_photo_count = len(photo_data) 58 59 # 开始下载并保存5K分辨率壁纸 60 for photo in photo_data: 61 62 # 创建一个文件夹存放我们下载的图片 63 if not exists(‘./‘ + str(type_id)): 64 makedirs(‘./‘ + str(type_id)) 65 66 # 准备下载的图片链接 67 file_url = photo[‘urls‘][‘raw‘] 68 69 # 准备下载的图片名称,不包含扩展名 70 file_name_only = file_url.split(‘/‘) 71 file_name_only = file_name_only[len(file_name_only) -1] 72 73 # 准备保存到本地的完整路径 74 file_full_name = ‘./‘ + str(type_id) + ‘/‘ + file_name_only 75 76 # 开始下载图片 77 Down_load(file_url, file_full_name, now_photo_count, all_photo_count) 78 now_photo_count = now_photo_count + 1 79 80 81 82 if __name__ == ‘__main__‘: 83 84 # 最新 1, 最热 2, 女生 3, 星空 4 85 # 爬取类型为3的图片(女生),一共准备爬取20000张 86 wall_paper_id = 1 87 wall_paper_count = 10 88 while(True): 89 90 # 换行符 91 print(‘\n\n‘) 92 93 # 选择壁纸类型 94 wall_paper_id = input("壁纸类型:最新壁纸 1, 最热壁纸 2, 女生壁纸 3, 星空壁纸 4\n请输入编号以便选择5K超清壁纸类型:") 95 # 判断输入是否正确 96 while(wall_paper_id != str(1) and wall_paper_id != str(2) and wall_paper_id != str(3) and wall_paper_id != str(4)): 97 wall_paper_id = input("壁纸类型:最新壁纸 1, 最热壁纸 2, 女生壁纸 3, 星空壁纸 4\n请输入编号以便选择5K超清壁纸类型:") 98 99 100 # 选择要下载的壁纸数量 101 wall_paper_count = input("请输入要下载的5K超清壁纸的数量:") 102 # 判断输入是否正确 103 while(int(wall_paper_count) <= 0): 104 wall_paper_count = input("请输入要下载的5K超清壁纸的数量:") 105 106 107 # 开始爬取5K高清壁纸 108 print("正在下载5K超清壁纸,请稍等……") 109 crawler_photo(int(wall_paper_id), int(wall_paper_count)) 110 print(‘\n下载5K高清壁纸成功!‘)
初学者对Python的概念模糊不清的,Python能做什么,学的时候该按照什么线路去学习,学完往哪方面发展,想深入了解,详情可以复制有道云笔记链接到浏览器打开了解:http://note.youdao.com/noteshare?id=e4fa02e7b56d7909a27674cdb3da08aa
学习视频资料、开发工具等都有免费分享,还有专业的老师答疑
标签:列表 决定 pat json input makedirs apple block 模糊
原文地址:https://www.cnblogs.com/ITbiancheng/p/12273240.html