作者:刘旭濠
这个功能是在与别人讨论的时候发现的,一开始还不知道还可以这样玩,最后讨论才知道,还可以爬取这些东西,最后不仅弄了爬图片,还可以爬别的直接上代码吧
# 获取一个网址 pip install requests
from requests import get
# 判断文件类型的第三方包 pip install filetype
from filetype import guess
# 命名文件或者目录
from os import rename
# 创建目录的包
from os import makedirs
# 判断文件是否存在
from os.path import exists
# 将已经编码的json字符串解码为python对象
from json import loads
# 上下文管理器操作模块 当我们请求api的时候,如果程序运行完成了,就关闭链接
from contextlib import closing
'''
1.获取图片资源
2.下载图片资源
'''
# 文件下载器 参数:1.资源链接 2.保存路径 3.当前下载数量 4.图片总数
def down_load(file_url, file_path, now_wallpaper_count, all_wallpaper_count):
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"
}
# 开始下载图片
with closing(get(file_url, headers=headers, stream=True)) as response:
# 单次请求最大值
chunk_size = 1024
# 文件总大小 这个参数