簡體   English   中英

如何使用#Python 從網站下載音樂文件

[英]How can I download music files from websites using #Python

  1. 如何使用#Python 從網站下載音樂文件

這個代碼

from bs4 import BeautifulSoup
from requests import *
import urllib

link = input("https://www.chosic.com/free-music/all/")

url = urllib.request.urlopen(link)
content = url.read()
soup = BeautifulSoup(content,'html.parser')
for audio in soup.find_all('audio'):
    print(len(audio))

Traceback (most recent call last):
  File "C:\Users\pc\Desktop\Downloads files from url using python .py", line 8, in <module>
    url = urllib.request.urlopen(link)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 501, in open
    req = Request(fullurl, data)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 320, in __init__
    self.full_url = url
  File "C:\Program Files\Python39\lib\urllib\request.py", line 346, in full_url
    self._parse()
  File "C:\Program Files\Python39\lib\urllib\request.py", line 375, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: ''

我想通過網站鏈接提取mp3和wav的鏈接請有人能幫助我

您可以使用下一個示例如何從該頁面下載所有mp3文件:

import requests
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0"
}

url = "https://www.chosic.com/free-music/all/"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

for u in soup.select("[data-url]"):
    u = u["data-url"]
    print("Downloading {}".format(u))
    with open(u.split("/")[-1], "wb") as f_out:
        f_out.write(requests.get(u, headers=headers).content)

印刷:

Downloading https://www.chosic.com/wp-content/uploads/2020/06/John_Bartmann_-_09_-_Happy_Clappy-1.mp3
Downloading https://www.chosic.com/wp-content/uploads/2020/11/batchbug-sweet-dreams.mp3
Downloading https://www.chosic.com/wp-content/uploads/2021/01/fm-freemusic-inspiring-optimistic-upbeat-energetic-guitar-rhythm.mp3
Downloading https://www.chosic.com/wp-content/uploads/2021/02/keys-of-moon-white-petals.mp3


...and so on.

並保存*mp3文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM