# !/usr/bin/python3
# -*- coding: utf-8 -*-
import re
from lxml import etree
import requests
import time
from tqdm import tqdm
import os
from urllib.request import urlopen
def download_from_url(url, dst):
"""
@param: url to download file
@param: dst place to put the file
:return: bool
"""
# 获取文件长度
try:
file_size = int(urlopen(url).info().get('Content-Length', -1))
except Exception as e:
print(e)
print("错误,访问url: %s 异常" % url)
return False
# print("file_size",file_size)
# 判断本地文件存在时
if os.path.exists(dst):
# 获取文件大小
first_byte = os.path.getsize(dst)
else:
# 初始大小为0
first_byte = 0
# 判断大小一致,表示本地文件存在
if first_byte >= file_size:
print("文件已经存在,无需下载")
return file_size
header = {"Range": "bytes=%s-%s" % (first_byte, file_size)}
pbar = tqdm(
total=file_size, initial=first_byte,
unit='B', unit_scale=True, desc=url.split('/')[-1])
# 访问url进行下载
req = requests.get(url, headers=header, stream=True)
try:
with(open(dst, 'ab')) as f:
for chunk in req.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
pbar.update(1024)
except Exception as e:
print(e)
return False
pbar.close()
return True
def DownloadFile(url, name):
"""
下载文件
:param url:
:param name:
:return:
"""
try:
resp = requests.get(url=url, stream=True)
content_size = int(resp.headers['Content-Length']) / 1024
with open(name, "wb") as f:
print("package total size is:", content_size, 'k,start...')
for data in tqdm(iterable=resp.iter_content(1024), total=content_size, unit='k', desc=name):
f.write(data)
print("%s 下载成功"%url)
return True
except Exception as e:
print(e)
print("%s 下载失败" % url)
return False
if __name__=='__main__':
# 头部
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}
download_from_url('https://shortv.cdp.qq.com/szg_4973_50001_0b6bz4aagaaajuappnkwkfqvdt6daphqaa2a.f612.mp4?dis_k=bf2737aba3e9527a1443fb1897aa9f6e&dis_t=1634261339&bid=kandian&network=2&seqid=1634261339949674', 'lol.mp4')
Python爬取qq看点视频
最新推荐文章于 2024-03-21 08:40:06 发布