python3获取文件中url内容并下载代码实例
作者:Done、 时间:2021-07-03 04:11:28
这篇文章主要介绍了python3获取文件中url内容并下载代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-12-25 11:33
# @Author : Anthony
# @Email : ianghont7@163.com
# @File : get_video_audio_file.py
import xlrd as xl
import requests
# 创建文件夹
def mkdir_floder(path):
import os
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
# print(path + ' 创建成功')
return True
else:
# 如果目录存在则不创建,并提示目录已存在
# print(path + ' 目录已存在')
return False
def request_floder(floder01,floder02,filename,url,types):
# 拼接全路径
all_path = basedirpath+floder01+'/'+floder02+'/'
res = requests.get(url.strip())
music = res.content
with open(all_path+filename+'.'+types, 'ab') as file: # 保存到本地的文件名
file.write(music)
file.flush()
def get_xls(filename):
# 打开文件
xls_file=xl.open_workbook(filename)
# 获取第一个sheet内容
xls_sheet=xls_file.sheets()[0]
# 总行数
line_nums = xls_sheet.nrows
for i in range(0,line_nums):
# 获取每一行的内容
row_value = xls_sheet.row_values(i)
# 获取第一级目录名称
folder1 = row_value[0]
# 获取第二级目录名称
folder2 = row_value[1]
# 获取文件名
file_name = row_value[2]
# 获取url内容
url = row_value[3]
# 创建目录
mkdir_floder(basedirpath+folder1+'/'+folder2)
# 转换url的类型为dict
url = eval(url)
# 获取视频内容
if "audio" in url:
if url["audioF"].strip() != "":
audio_url = url["audioF"]
audio_url_end = audio_url.split('.')[-1]
print(audio_url)
request_floder(folder1, folder2, file_name, audio_url, audio_url_end)
elif url["audio"].strip() != "":
audio_url = url["audio"]
audio_url_end = audio_url.split('.')[-1]
print(audio_url)
request_floder(folder1, folder2, file_name, audio_url, audio_url_end)
# 获取音频内容
elif "video" in url:
if url["video"].strip() != "":
video_file = url["video"]
video_file_end = video_file.split('.')[-1]
print(video_file)
request_floder(folder1, folder2, file_name, video_file, video_file_end)
if __name__ == "__main__":
# 文件存放路径
basedirpath = "/Users/ianthony/Desktop/Devops/"
# 读取的xlsx文件
get_xls("001.xlsx")
来源:https://www.cnblogs.com/ipyanthony/p/12096828.html
标签:python,url,下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
再读《你是一个职业的页面重构工作者吗?》
2009-02-11 12:22:00
Python文件处理与垃圾回收机制详情
2023-06-14 16:36:17
基于Python实现音乐播放器的实现示例代码
2023-11-26 23:58:50
利用python实现PSO算法优化二元函数
2022-06-17 20:53:04
![](https://img.aspxhome.com/file/2023/5/70225_0s.jpg)
ASP如何获取通过代理的真实IP地址
2007-09-20 13:11:00
BOF、EOF 属性
2009-05-11 12:37:00
list视图方式设计浅析
2008-12-21 16:04:00
![](https://img.aspxhome.com/file/UploadPic/200812/21/01-28s.jpg)
python实现大转盘抽奖效果
2023-12-16 13:50:30
![](https://img.aspxhome.com/file/2023/1/72081_0s.jpg)
教你使用SQL Server数据库进行网络链接
2009-01-13 13:41:00
Python安装第三方库攻略(pip和Anaconda)
2023-02-01 22:16:41
![](https://img.aspxhome.com/file/2023/1/65021_0s.png)
LZ77 算法的JS实现
2010-04-11 22:32:00
基于Python实现的扫雷游戏实例代码
2023-06-26 12:29:34
解决pip安装的第三方包在PyCharm无法导入的问题
2022-10-18 11:20:27
![](https://img.aspxhome.com/file/2023/8/72818_0s.png)
python实现贪吃蛇双人大战
2022-10-10 19:18:37
![](https://img.aspxhome.com/file/2023/1/69561_0s.jpg)
如何编写TOP10之类的排行榜?
2009-11-07 18:45:00
老生常谈Python startswith()函数与endswith函数
2023-06-03 02:02:42
Django框架视图介绍与使用详解
2021-03-29 08:50:46
![](https://img.aspxhome.com/file/2023/1/80741_0s.png)
python开发的自动化运维工具ansible详解
2022-12-11 08:40:00
![](https://img.aspxhome.com/file/2023/8/64298_0s.jpg)
分布式DBA:SQL存储过程知识总结
2009-02-24 17:17:00
利用Python实现学生信息管理系统的完整实例
2022-03-12 10:35:03
![](https://img.aspxhome.com/file/2023/9/79619_0s.png)