[英]Why can't I play the MIDI files I have downloaded programmatically, but I can play them when I download them manually?
I want to download the MIDI files from this website for a project. 我想从这个网站下载一个项目的MIDI文件。 I have written the following code to download the files:
我编写了以下代码来下载文件:
from bs4 import BeautifulSoup
import requests
import re, os
import urllib.request
import string
base_url = "http://www.midiworld.com/files/"
base_path = 'path/where/I/will/save/the/downloaded/MIDI/files'
os.chdir(base_path + '/MIDI Files')
for i in range(1,2386):
page = requests.get(base_url + str(i))
soup = BeautifulSoup(page.text, "html.parser")
li_box = soup.select("div ul li a")
urllib.request.urlretrieve(base_url+str(i), str(i)+'.mid')
This is downloading the files, but when I click on them to play, they don't play; 这是下载文件,但当我点击它们播放时,它们不播放; I get this error:
我收到此错误:
But if I download the files manually (I checked for a couple of them), I can play the files. 但是,如果我手动下载文件(我检查了几个),我可以播放文件。 In case its relevant, those files also have different names, not numbers like how I am saving them.
如果它是相关的,那些文件也有不同的名称,而不是像我如何保存它们的数字。 Could it be the cause for this?
这可能是原因吗? The files are not empty too, as can be seen from this screenshot below:
这些文件也不是空的,如下面的截图所示:
EDIT: When I tried to load a programmatically downloaded MIDI file to compare it to its corresponding manually downloaded MIDI file in this website, I got this error: 编辑:当我试图加载一个程序下载的MIDI文件,以将其与该网站上相应的手动下载的MIDI文件进行比较时,我收到此错误:
Failed to load data=error
But no such error when loading the manually downloaded one. 但加载手动下载的错误时没有这样的错误。
EDIT 2: These are the first 50 bytes of the hex dump: 编辑2:这是十六进制转储的前50个字节:
For the programmatically downloaded file: 对于以编程方式下载的文件:
file name: 1.mid
mime type:
0000-0010: 3c 21 44 4f-43 54 59 50-45 20 68 74-6d 6c 20 50 <!DOCTYP E.html.P
0000-0020: 55 42 4c 49-43 20 22 2d-2f 2f 57 33-43 2f 2f 44 UBLIC."- //W3C//D
0000-0030: 54 44 20 58-48 54 4d 4c-20 31 2e 30-20 53 74 72 TD.XHTML .1.0.Str
0000-0032: 69 63
For the corresponding manually downloaded file: 对于相应的手动下载文件:
file name: Adson_John_-_Courtly_Masquing_Ayres.mid
mime type:
0000-0010: 4d 54 68 64-00 00 00 06-00 01 00 0b-00 f0 4d 54 MThd.... ......MT
0000-0020: 72 6b 00 00-00 7b 00 ff-58 04 04 02-18 08 00 ff rk...{.. X.......
0000-0030: 59 02 00 00-00 ff 51 03-07 a1 20 f0-40 ff 51 03 Y.....Q. ....@.Q.
0000-0032: 09 27
Your code works fine, just change base_url to 您的代码工作正常,只需将base_url更改为
base_url = "http://www.midiworld.com/download/"
Right now, ie "1.mid" contains the HTML for this site: http://www.midiworld.com/files/1 (You can open it with a text editor.) 现在,即“1.mid”包含此站点的HTML: http : //www.midiworld.com/files/1 (您可以使用文本编辑器打开它。)
The MIDI-files can be downloaded the url http://www.midiworld.com/download/ {insert number} MIDI文件可以下载到网址http://www.midiworld.com/download/ {insert number}
I downloaded the first 100 but it seems there are currently 4992 downloadable midi files, so if you want more files, just change 我下载了前100个,但似乎目前有4992个可下载的midi文件,所以如果你想要更多文件,只需更改即可
for i in range(1,4992):
As a side-note, the site gives you download "_-_.mid" which is 0 bytes, if the requested .mid doesn't exist. 作为旁注,如果请求的.mid不存在,该站点将为您提供下载“_-_.mid”,即0字节。 So, if you are going to repeat downloading the files and you want all the files they have, consider setting range to for example 100 000 and break the loop if downloaded file-size is 0 bytes.
因此,如果您要重复下载文件并且想要他们拥有的所有文件,请考虑将范围设置为例如100 000,如果下载的文件大小为0字节则中断循环。
for i in range(1,100000):
if (urllib.request.urlopen(base_url+str(i)).length == 0):
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.