亲宝软件园·资讯

展开

Python 爬取音频下载 python爬取音频下载的代码实例

Gg、 人气:0
想了解python爬取音频下载的代码实例的相关内容吗,Gg、在本文为您仔细讲解Python 爬取音频下载的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python,爬取音频,python,爬取音频下载,下面大家一起来学习吧。

抓取“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()

爬取结果

加载全部内容

相关教程
猜你喜欢
用户评论