Python Web Scraping Tutorial: Download MP3 Files with requests and BeautifulSoup

This tutorial explains how to use Python's requests and BeautifulSoup libraries to fetch a webpage, parse its anchor tags, filter and construct download URLs, and automatically download multiple MP3 files by writing the binary content to local files.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Web Scraping Tutorial: Download MP3 Files with requests and BeautifulSoup

The article introduces the required libraries for the task: requests for sending HTTP requests and BeautifulSoup for flexible HTML parsing, and shows how to install them via pip install requests and pip install BeautifulSoup, then import with import requests and from bs4 import import BeautifulSoup as bf.

The target site is a page that requires manual clicks to download MP3 files, making manual download impractical when hundreds of files are needed.

Step 1: Use requests.get('http://www.goodkejian.com/ertonggushi.htm') to obtain the page source. All download links are stored in tags with a fixed length; the "amp;" part must be removed before the link can be used directly.

Step 2: Parse the HTML with soup = bf(r.text, 'html.parser') and extract all elements via res = soup.find_all('a') .

Step 3 (Download): Iterate over the extracted tags, convert each to a string, filter by length (126 characters), slice the string to build the correct URL, request the file with requests.get(url), and write the binary content to a local .rar file using a counter for filenames.

Full code example:

import requests<br/>from bs4 import BeautifulSoup as bf<br/><br/>r = requests.get('http://www.goodkejian.com/ertonggushi.htm')<br/><br/>soup = bf(r.text, 'html.parser')<br/>res = soup.find_all('a')<br/><br/>recorder = 1<br/># Length 126 identifies the desired link<br/>for i in res:<br/>    dst = str(i)<br/>    if dst.__len__() == 126:<br/>        url1 = dst[9:53]<br/>        url2 = dst[57:62]<br/>        url = url1 + url2<br/>        print(url)<br/>        xjh_request = requests.get(url)<br/>        with open("./res/" + str(recorder) + ".rar", 'wb') as file:<br/>            file.write(xjh_request.content)<br/>        file.close()<br/>        recorder += 1<br/>        print("ok")

The article concludes with a disclaimer that the content is collected from the web and the original author retains copyright.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

beautifulsoupweb-scrapingmp3-download
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.