python爬取音頻下載的示例代碼
抓取“xmly”鬼故事音頻
import json # 在這個url,音頻鏈接為JSON動態(tài)生成,所以用到了json模塊import requestsheaders = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'}# 請求網(wǎng)頁def open_url(url): r = requests.get(url, headers=headers) r.encoding = ’utf-8’ html = r.text # 將JSON轉(zhuǎn)化成字符串 html = json.loads(html) return html# 得到所有音頻的鏈接def get_urls(url): us = url[’data’][’tracksAudioPlay’] return usdef main(): num = 1 url = ’https://www.ximalaya.com/revision/play/album?albumId=4256765&pageNum=1&sort=-1&pageSize=30’ html = open_url(url) urls = get_urls(html) # 遍歷得到每個音頻的鏈接和對應(yīng)的名稱 for ul in urls: uls = ul[’src’] filename = ul[’trackName’] reponse = requests.get(uls).content with open(filename + ’.m4a’, ’wb’) as file: file.write(reponse) print(str(num) + ’. ’ + filename) num += 1if __name__ == ’__main__’: main()
爬取結(jié)果

以上就是python爬取音頻下載的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python 爬取音頻下載的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. .net中string類型可以作為lock的鎖對象嗎2. 新手學(xué)python應(yīng)該下哪個版本3. 詳細(xì)總結(jié)Java for循環(huán)的那些坑4. python 使用Tensorflow訓(xùn)練BP神經(jīng)網(wǎng)絡(luò)實現(xiàn)鳶尾花分類5. jsp文件下載功能實現(xiàn)代碼6. python對批量WAV音頻進(jìn)行等長分割的方法實現(xiàn)7. Java進(jìn)行Appium自動化測試的實現(xiàn)8. uni-app結(jié)合PHP實現(xiàn)單用戶登陸demo及解析9. ajax實現(xiàn)頁面的局部加載10. 如何利用Python matplotlib繪制雷達(dá)圖

網(wǎng)公網(wǎng)安備