python下载英语听力资料

下载地址:https://www.tingclass.net/

第一版,单线程下载

# coding=utf-8import requests
import os
# import shutildef downLrc():root = os.getcwd()KEY_LRC = ".lrc"KEY_MP3 = ".mp3"urlMap = {KEY_LRC: "http://down11.tingclass.com/textrar/lesson/0000/72/1.lrc",KEY_MP3: "http://down010701.tingclass.com/lesson/shi0529/0000/72/1.mp3"}arrNot = []dirPath = root  # os.path.sep.join((root, "新概念英语第一册"))# if not os.path.isdir(dirPath):#     os.makedirs(dirPath)# if os.path.isdir(dirPath):#     try:#         for k in ["down_lrc.py", "down_lrc.bat"]:#             src = os.path.sep.join((root, k))#             shutil.copy(src, dirPath)#     except Exception as e:#         print("error e = {}".format(e))for i in range(1, 13):for suffix, urlFile in urlMap.items():prefix = urlFileif urlFile.endswith(".lrc") or urlFile.endswith(".mp3"):prefix = os.path.dirname(urlFile)nn = str(i)  # .zfill(2)name = nn + suffixfpath = os.path.sep.join((dirPath, name))realUrl = "/".join((prefix, name))if not os.path.isfile(fpath):r = requests.get(realUrl)if r.status_code == 200:with open(fpath, "wb") as f:f.write(r.content)print("ok: {}".format(realUrl))else:print("不存在: {}".format(name))arrNot.append(name)continueelse:continueif arrNot:print("无效的下载地址:{}".format(arrNot))else:print("下载成功")if __name__ == "__main__":downLrc()

第二版,多线程下载:

# coding=utf-8import requests
import os
import concurrent
from concurrent.futures import ThreadPoolExecutor
# import shutilgPool = ThreadPoolExecutor(50)def downOne(realUrl, fpath, name, arrNot):r = requests.get(realUrl)if r.status_code == 200:with open(fpath, "wb") as f:f.write(r.content)print("ok: {}".format(realUrl))return Trueelse:print("不存在: {}".format(name))arrNot.append(name)return Falsedef downLrc():root = os.getcwd()KEY_LRC = ".lrc"KEY_MP3 = ".mp3"urlMap = {KEY_LRC: "http://down11.tingclass.com/textrar/lesson/0000/25/1.lrc",KEY_MP3: "http://down010701.tingclass.com/lesson/shi0529/0000/25/1.mp3"}arrNot = []ts = []dirPath = rootfor i in range(1, 70):for suffix, urlFile in urlMap.items():prefix = urlFileif urlFile.endswith(".lrc") or urlFile.endswith(".mp3"):prefix = os.path.dirname(urlFile)nn = str(i)  # .zfill(2)name = nn + suffixfpath = os.path.sep.join((dirPath, name))realUrl = "/".join((prefix, name))if not os.path.isfile(fpath):fu = gPool.submit(downOne, realUrl, fpath, name, arrNot)ts.append(fu)else:continueprint("wait {}".format(concurrent.futures.wait(ts)))if arrNot:print("无效的下载地址:{}".format(arrNot))else:print("下载成功")if __name__ == "__main__":downLrc()

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部