首页 > 编程语言 > 详细

python爬虫之爬取音频

时间:2020-09-27 21:46:16      阅读:45      评论:0      收藏:0      [点我收藏+]

抓取“xmly”鬼故事音频

import json    # 在这个url,音频链接为JSON动态生成,所以用到了json模块
import requests
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
}


# 请求网页
def open_url(url):
    r = requests.get(url, headers=headers)
    r.encoding = utf-8
    html = r.text
    # 将JSON转化成字符串
    html = json.loads(html)
    return html


# 得到所有音频的链接
def get_urls(url):
    us = url[data][tracksAudioPlay]
    return us


def 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)
    # 遍历得到每个音频的链接和对应的名称
    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 += 1

if __name__ == __main__:
    main()

python爬虫之爬取音频

原文:https://www.cnblogs.com/jsxxd/p/13740912.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!