使用python查找页面中的所有音频链接

2024-06-16 10:08:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在使用Python的站点中查找所有音频文件,如.mp3、.wav、.ogg、.wma等。 这是我的代码>;>;

    url = urllib.request.urlopen(link)
    content = url.read()
    soup = BeautifulSoup(content)
    links = [a['href'] for a in soup.find_all('a',href=re.compile('http.*\.mp3'))]
    print (str(len(links)) + " Audios Found ")
   # print (links)
    print("\n".join(links))

只能找到.mp3链接。 我还需要其他音频链接。


Tags: 代码gturl站点链接linkscontentmp3
1条回答
网友
1楼 · 发布于 2024-06-16 10:08:31

因为您使用regex来选择链接,所以请更改此行

links = [a['href'] for a in soup.find_all('a',href=re.compile('http.*\.mp3'))]

links = [a['href'] for a in soup.find_all('a',href=re.compile('http.*\.(mp3|wav|ogg|wma)'))]

相关问题 更多 >