-- coding:utf-8 --
from requests import get
from filetype import guess
from os import rename
from os import makedirs
from os.path import exists
from json import loads
from contextlib import closing
文件下载器
def Down_load(file_url, file_full_name, now_photo_count, all_photo_count):
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"}
开始下载图片
with closing(get(file_url, headers=headers, stream=True)) as response:
chunk_size = 1024 # 单次请求最大值
content_size = int(response.headers['content-length']) # 文件总大小
data_count = 0 # 当前已传输的大小
with open(file_full_name, "wb") as file:
for data in response.iter_content(chunk_size=chunk_size):
file.write(data)
done_block = int((data_count / content_size) * 50)
data_count = data_count + len(data)
now_jd = (data_count / content_size) * 100
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=" ")
下载完图片后获取图片扩展名,并为其增加扩展名
file_type = guess(file_full_name)
rename(file_full_name, file_full_name + '.' + file_type.extension)
爬取不同类型图片
def crawler_photo(type_id, photo_count):
最新 1, 最热 2, 女生 3, 星空 4
if(type_id == 1):
url = 'service.paper.meiyuan.in/api/v2/colu…' + str(photo_count)
elif(type_id == 2):
url = 'service.paper.meiyuan.in/api/v2/colu…' + str(photo_count)
elif(type_id == 3):
url = 'service.paper.meiyuan.in/api/v2/colu…' + str(photo_count)
elif(type_id == 4):
url = 'service.paper.meiyuan.in/api/v2/colu…' + str(photo_count)
获取图片列表数据
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"}
respond = get(url, headers=headers)
photo_data = loads(respond.content)
已经下载的图片张数
now_photo_count = 1
所有图片张数
all_photo_count = len(photo_data)
开始下载并保存5K分辨率壁纸
for photo in photo_data:
创建一个文件夹存放我们下载的图片
if not exists('./' + str(type_id)):
makedirs('./' + str(type_id))
准备下载的图片链接
file_url = photo['urls']['raw']
准备下载的图片名称,不包含扩展名
file_name_only = file_url.split('/')
file_name_only = file_name_only[len(file_name_only) -1]
准备保存到本地的完整路径
file_full_name = './' + str(type_id) + '/' + file_name_only
开始下载图片
Down_load(file_url, file_full_name, now_photo_count, all_photo_count)
now_photo_count = now_photo_count + 1
if name == 'main':
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。