导入包,并新建相声文件夹
import requests
import os
from lxml import etree
import json
import time
b_path = 'e:/郭德纲相声'
if not os.path.exists(b_path):
os.mkdir(b_path)
获取所下载种类的最大页数
def get_num(a):
url = f"https://www.ximalaya.com/xiangsheng/9723091/p{a}/"
headers ={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'
}
response = requests.get(url,headers=headers)
html = etree.HTML(response.text)
num = html.xpath('//div[@class="pagination _Qp"]//a/span/text()')[-1]
return num,html
根据返回最大页数,调用函数
def get_page(num,html):
for i in range(1,int(num)+1):
n1,html = get_num(i)
download_data(html)
time.sleep(2)
获取每个页面上单个相声的文件名及下载音频链接编码
def download_data(html):
v_name = html.xpath('//div[@class="sound-list _Qp"]/ul//a/span/text()')