Python3按钮触发文件下载:非requests/urllib获取MIDI文件方案咨询
Fix for Downloading MIDI Files from freemidi.org Getter URLs
Hey there! I’ve dealt with this exact problem before—those /getter-XXX links on freemidi.org aren’t direct file URLs. They’re intermediate landing pages that handle the download flow, which is why you’re grabbing HTML instead of a .mid file. Here’s how to work around it with both libraries you mentioned:
Using requests (Simpler Approach)
First, you’ll need to parse the getter page to extract the actual MIDI download link. We’ll use BeautifulSoup for HTML parsing (install it first with pip install beautifulsoup4):
import requests from bs4 import BeautifulSoup # Replace with your getter URL getter_url = "https://freemidi.org/getter-13560" # Add a user-agent to avoid being blocked by anti-scraping measures headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" } # Fetch the getter page response = requests.get(getter_url, headers=headers) soup = BeautifulSoup(response.text, "html.parser") # Find the actual download link (the button's href on the getter page) # If the class name changes, inspect the download button element to get the right selector download_btn = soup.find("a", class_="download-btn") if not download_btn: # Fallback: look for any link containing "/download/" in the URL download_btn = soup.find("a", href=lambda href: href and "/download/" in href) full_download_url = f"https://freemidi.org{download_btn['href']}" # Download the MIDI file midi_response = requests.get(full_download_url, headers=headers) with open("your_file.mid", "wb") as f: f.write(midi_response.content)
Using urllib.request
If you prefer sticking to the standard library, here’s how to do it (still need BeautifulSoup for parsing):
import urllib.request from bs4 import BeautifulSoup getter_url = "https://freemidi.org/getter-13560" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" } # Create a request with headers to avoid blocking req = urllib.request.Request(getter_url, headers=headers) with urllib.request.urlopen(req) as response: html = response.read().decode("utf-8") soup = BeautifulSoup(html, "html.parser") download_btn = soup.find("a", class_="download-btn") or soup.find("a", href=lambda href: href and "/download/" in href) full_download_url = f"https://freemidi.org{download_btn['href']}" # Download the file urllib.request.urlretrieve(full_download_url, "your_file.mid")
Key Notes:
- Anti-Scraping: Always include a valid
User-Agentheader—freemidi.org blocks requests without one, which might lead to empty or blocked responses. - Selector Changes: If the
download-btnclass stops working, right-click the download button on the getter page, select "Inspect", and check the element’s attributes to update the selector. - Why This Works: The getter page exists to track downloads or show ads before letting users grab the file. By extracting the direct download link from that page, you bypass the intermediate HTML step.
内容的提问来源于stack exchange,提问作者Semyon Kirekov
相关产品推荐
相关产品推荐

