为什么不能播放以编程方式下载的MIDI文件,但是当我手动下载它们时可以播放它们? [英] Why can't I play the MIDI files I have downloaded programmatically, but I can play them when I download them manually?

查看:87
本文介绍了为什么不能播放以编程方式下载的MIDI文件,但是当我手动下载它们时可以播放它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从此网站下载一个项目的MIDI文件.我编写了以下代码来下载文件:

I want to download the MIDI files from this website for a project. 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:

编辑:当我尝试加载以编程方式下载的MIDI文件以将其与网站,出现此错误:

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:

Failed to load data=error

但是在加载手动下载的文件时没有这种错误.

But no such error when loading the manually downloaded one.

:十六进制转储的前50个字节:

EDIT 2: These are the first 50 bytes of the hex dump:

以编程方式下载的文件:

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

推荐答案

您的代码运行正常,只需将base_url更改为

Your code works fine, just change base_url to

base_url = "http://www.midiworld.com/download/"

现在,即"1.mid"包含此站点的HTML: http://www .midiworld.com/files/1 (您可以使用文本编辑器将其打开.)

Right now, i.e. "1.mid" contains the HTML for this site: http://www.midiworld.com/files/1 (You can open it with a text editor.)

可以通过以下网址下载MIDI文件: http://www.midiworld.com/download/ {插入编号}

The MIDI-files can be downloaded the url http://www.midiworld.com/download/{insert number}

我下载了前100个文件,但似乎目前有4992个可下载的midi文件,因此,如果您想要更多文件,只需更改

I downloaded the first 100 but it seems there are currently 4992 downloadable midi files, so if you want more files, just change

for i in range(1,4992):

作为一个旁注,如果所请求的.mid不存在,则该站点将为您下载0字节的"_-_.mid".因此,如果要重复下载文件,并希望它们具有所有文件,请考虑将范围设置为例如100 000,如果下载的文件大小为0字节,则中断循环.

As a side-note, the site gives you download "_-_.mid" which is 0 bytes, if the requested .mid doesn't exist. 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.

for i in range(1,100000):
    if (urllib.request.urlopen(base_url+str(i)).length == 0):
        break

这篇关于为什么不能播放以编程方式下载的MIDI文件,但是当我手动下载它们时可以播放它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆