一般在使用爬虫爬取一些数据的时候,我们一般不关注其爬取数据的过程,当程序运行结束之后我们去看结果就行了,但当我们想要知道每条数据爬取的进度的时候该如何处理呢?本次博客就为大家分享如何实现一只有下载进度条的小爬虫
在这之前,我们需要先了解一些新的知识内容
一下就贴上实现进度条的部分的代码,供大家参考:
def save_video():
video = requests.get(url=download, headers=header, stream=True)
# Setting per chunk size is 1024
chunk_size = 1024
# Get size of video that will be download
size = 0
# name = "".join(random.sample(string.digits, 4))
video_size = int(video.headers['content-length'])
print("Start to download number %d video" % (k+1))
print("Video size is : " + str(round(float(video_size / chunk_size / 1024), 4)) + "[MB]")
print("\n")
with open(storage_path + os.sep + "%s.mp4" % name_list[k], "wb") as v:
for data in video.iter_content(chunk_size=chunk_size):
v.write(data)
size = len(data) + size
print('\r' + "Downloading : " + int(size / video_size * 100) * ">" + "【" + str(
round(size / chunk_size / 1024, 2))
+ "MB】" + "【" + str(round(float(size / video_size) * 100, 2)) + "%" + "】", end="") # 加上end表示运行完后不换行
# print("\n")
start = datetime.datetime.now()
print("Start download...")
save_video()
print("\n Download End...")
end = datetime.datetime.now()
print("Spend Times:" + str(end - start).split('.')[0] + " Seconds")