求大神教一下,如何批量下载这个网页里面的音频文件 ,找了论坛其他帖子,好像跟我这个情况都不一样。 网页进去后,每个选项点进去是可以试听一小段音频的,完整版是需要登录账号付费才能下载,但是我只需要下载小段即可,而且是需要批量下载这个网页里面的所有音频文件,最好操作有方法,求教,感谢了! 网页如下: http://www.karasolo.com/bzxz/bzxz.aspx 网页, 批量
[Python] 纯文本查看 复制代码from bs4 import BeautifulSoup import requests import time import sys import re print("begin!") url = "http://www.karasolo.com/bzxz/bzxz.aspx" # 替换成要下载的地址 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" } web = "http://www.karasolo.com/bzxz/" r = requests.get(url,headers=headers) r.encoding = r.apparent_encoding r.encoding = 'uft-8' if r.status_code != 200: print("响应状态码不为200") soup = BeautifulSoup(r.text,"html.parser") ul = soup.find_all("a", attrs={"class":'a-wgd a-h240 color'}) for n in ul: href = web + n.attrs['href'] resp = requests.get(href, headers = headers) soup2 = BeautifulSoup(resp.text,"html.parser") title = soup2.find("div", attrs={"class":'h1_tit'}).text print("当前名字是" + title + "当前歌曲页面是: " + href) link = "http://www.karasolo.com" + re.search(r"mp3=(.*?)&", resp.text).group(1) song = requests.get(link, headers=headers).content with open(title + ".mp3", "wb") as f: f.write(song) time.sleep(1) print("全部下载完成!")