利用Python下载懒人听书音频保存,纯干货不废话!
生活随笔
收集整理的這篇文章主要介紹了
利用Python下载懒人听书音频保存,纯干货不废话!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用Python爬到音頻下載地址,再用Internet Download Manager批量自動下載
# -*- coding: utf-8 -*- """ Created on Fri Dec 14 21:07:11 2018 @author: fuwen """ from subprocess import call import os,requests, base64, json, time import jsonpath import html from bs4 import BeautifulSoup type=1 #https://www.lrts.me/album/400485 bookid BookID = 99144 # Sections=0 #一共多少章節 Chapters=92 #保存路徑 FilePath = 'd:\\mp3' #刪除文件夾所有文件,并且重新建立 def delDir(TitleName):global FilePathFilePath=FilePath+"\\"+TitleNameif os.path.exists(FilePath) is True:os.system('RD /q /s ' + FilePath)print()os.mkdir(FilePath)#使用IDM安裝下載 IdmPath = 'd:\\Internet Download Manager\\IDMan.exe' #關閉autoit 編寫程序檢測彈出框點否程序 exe_name="lrts.exe" def kill_exe(exe_name):"""殺死exe進程:param exe_name:進程名字:return:無"""os.system('taskkill /f /t /im '+exe_name)#MESMTPC.exe程序名字print("殺死進程{}".format(exe_name)) def get_json_value(json_data,key_name):'''獲取到json中任意key的值,結果為list格式'''# key_value = jsonpath.jsonpath(json_data, '$..{key_name}'.format(key_name=key_name))# https://blog.csdn.net/cling_snail/article/details/80980296key_value = jsonpath.jsonpath(json_data, '$[*].{key_name}'.format(key_name=key_name))#key的值不為空字符串或者為empty(用例中空固定寫為empty)返回對應值,否則返回emptyreturn key_value def getChapterTitle(html):soup = BeautifulSoup(html, 'html.parser')tilename = soup.find('h1', attrs={"class": "nowrap"}) # 查找span class為red的字符串return tilename.text def IdmDownLoad(DownloadUrl, Mp3Name):call([IdmPath, '/d',DownloadUrl,'/p',FilePath,'/f',Mp3Name,'/n']) def IdmDownLoadChangeName(DownloadUrl, Mp3Name):# call([IdmPath, '/d',DownloadUrl,'/p',FilePath,'/n']) begin=DownloadUrl.rfind('/')end=DownloadUrl.rfind('?')src_name=DownloadUrl[begin+1:end]os.rename(FilePath+"\\"+src_name,FilePath+"\\"+Mp3Name) def ChangeFileName(filename):filename = filename.replace('\\','')filename = filename.replace('/','')filename = filename.replace(':','')filename = filename.replace('*','')filename = filename.replace('“','')filename = filename.replace('”','')filename = filename.replace('<','')filename = filename.replace('>','')filename = filename.replace('|','')filename = filename.replace('?','?')filename = filename.replace('(','(')filename = filename.replace(chr(65279),'') # UTF-8+BOM # print(ord(filename[0]))filename = filename.split('(')[0]return html.unescape(filename) if __name__ == "__main__":Mp3ListJsonUrl = 'https://www.lrts.me/ajax/album/{0}/{1}/{2}'.format(BookID,Sections,Chapters)headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'}TitleUrl = 'https://www.lrts.me/album/{0}'.format(BookID)conn = requests.session()Mp3ListDic={}Mp3ListJson = conn.get(Mp3ListJsonUrl, headers = headers)Mp3TitleJson = conn.get(TitleUrl, headers = headers)titlename=getChapterTitle(Mp3TitleJson.text)if titlename is None:print("沒找到這個題目")exit() print(titlename)delDir(titlename)Mp3ListJson = json.loads(Mp3ListJson.text)# print(Mp3ListJson["data"]["data"])Josndata=Mp3ListJson["data"]["data"]Mp3IdList=get_json_value(Josndata,"id")# print(Mp3IdList)Mp3IdNameList=get_json_value(Josndata,"name")for Item in Mp3IdNameList:index=Mp3IdNameList.index(Item)Mp3ListDic[html.unescape(Item)]=Mp3IdList[index]# Mp3ListDic[Mp3IdList[index]]=html.unescape(Item)# print(Mp3ListDic)Mp3List = JosndataMp3NameList = [Mp3dict['name'] for Mp3dict in Mp3List]Mp3NameList = [ChangeFileName(i) for i in Mp3NameList]# print(Mp3NameList)AlreadyDown = [FileName.replace('.mp3','') for FileName in os.listdir(FilePath)]Count = 0os.startfile(exe_name)time.sleep(2)for Mp3Name in Mp3NameList :Count+=1# if Count==2:# breakif Mp3Name in AlreadyDown :continue#type=1&resourcesid=99144§ions=1\Mp3JsonUrl="https://www.lrts.me/ajax/playlist/{0}/{1}/{2}".format(type,BookID,Count)Mp3Url = conn.get(Mp3JsonUrl, headers = headers)# print(Mp3Url.text)html=Mp3Url.textsoup = BeautifulSoup(html, 'html.parser')mp3id="section"+str(Mp3ListDic[Mp3Name])print(mp3id)s1 = soup.find('li', attrs={"id": mp3id}) # 查找span class為red的字符串DownloadUrl = s1.find('input')["value"]print(DownloadUrl)try :IdmDownLoad(DownloadUrl,Mp3Name+'.mp3')time.sleep(2)except :print('%s,未購買,跳過……'%Mp3Name)kill_exe(exe_name) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137再用autoit寫個程序關閉一下idm煩人的提示框
$winTitle="Internet Download Manager" While 1$winHandle = WinGetHandle($winTitle);ConsoleWrite(WinGetTitle($winHandle))If WinGetTitle($winHandle)=="Internet Download Manager" ThenWinActivate($winHandle)EndIfIf WinWaitActive("Internet Download Manager") Then;ConsoleWrite($winHandle)ControlClick($winHandle, "", "[ID:7]")EndIfSleep(100)WEnd?
總結
以上是生活随笔為你收集整理的利用Python下载懒人听书音频保存,纯干货不废话!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光纤耦合透镜的参数优化
- 下一篇: 计算机音乐制作的论文,电脑音乐制作的相关