Download Python music, Xiaobai can also write crawler

Time:2020-8-18

Using Python to download music, Xiaobai can also write crawlers

**
Introduction: use beautifulsoup and request modules to grab and parse,
Finally save the music (Note: music quality is ordinary quality)
Here by the way, I would like to recommend a full range of resources for Python learning Seven clothes nine seven seven bar five (Digital homophony) conversion can be found, here are senior programmers to share previous learning experience, learning notes, as well as work experience of first-line enterprises, and carefully collate a copy of Python zero foundation to project actual combat materials, explain the latest technology of python, prospects, learning need to leave a message of small details
For module installation, open the CMD input

PIP install BS4 / / install beautifulsoup
PIP install requests / / install requests
pip install fake_ Useragent / / this module can generate a header randomly

(unable to install. Please upgrade pip or open CMD in administrator mode)

What we crawl here is Netease cloud music

https://music.163.com/artist?id=4292 //Crawled links
http://music.163.com/song/media/outer/url?id= //Music playing link

First of all: we get the source code of the webpage first

https://music.163.com/#/artist?id=4292
In the beginning, we used this link to request the web page directly, but we will find that the returned element of the URL is empty (ා). This link is not really a song list link.

But after searching, we will find that there is a different web link in the source

Next we’ll find the link https://music.163.com/song?id=1407551413 To see if it’s different. With the original link on a ‘/ ා’ difference, the content is not the same, this is Netease cloud hiding the source page.
Then look closely to find the music ID and name

The next step is code implementation

import requests
from fake_useragent import UserAgent
from bs4 import BeautifulSoup
import time

def get_html():
url = ‘https://music.163.com/artist?id=4292’
headers = {
‘user agent’: useragent (). Random ා random a request header of imitating browser
}
response = requests.get(url, headers=headers)
res = BeautifulSoup(response.text, ‘lxml’)
id_lists = res.find(class_=’f-hide’).find_all(‘a’)
return id_lists

def download(names,hrefs):
#After getting the music ID, it needs to be parsed and saved
headers = {
‘User-Agent’: UserAgent().random
}
#We need to add a header here, otherwise it will be false data
url = ‘http://music.163.com/song/media/outer/url?id=’
#Netease cloud chain address, through this can be downloaded for free
response = requests.get(url+hrefs,headers=headers).content
#Return binary
F = open (‘e: \ \ \ {}. MP3 ‘. Format (names),’wb’). The songs downloaded here are stored in the music folder of e disk
f.write(response)
Print (‘downloading {} ‘. Format (names))

if __name__ == ‘__main__’:
get_html()
for id_url in get_html():
names = id_url.text
hrefs = id_url[‘href’][9:]
#Use the variable to receive the song name and ID
download(names,hrefs)
time.sleep (1) Sleep for one second to prevent frequent visits

If you want to download other songs, you can change the link in the URL, pay attention to delete ‘/ #’ to get the real link. This code applies to song lists and albums
Finally, by the way, I would like to recommend a complete set of resources for Python learning Seven clothes nine seven seven bar five (Digital homophony) conversion can be found, here are senior programmers to share previous learning experience, learning notes, as well as work experience of first-line enterprises, and carefully collate a copy of Python zero foundation to project actual combat materials, explain the latest technology of python, prospects, learning need to leave a message of small details

The text and pictures of this article are from the Internet and my own ideas. They are for study and communication only. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

Recommended Today

Construction of Vue development environment and project creation under mac

start First of all, let’s take a look at the overall development environment of Vue. Homebrew: package manager under MAC system, similar to apt get under Linux, windows control panel – install and remove program Node.jsJavaScript runtime, which is similar to simultaneous interpretation in international conferences, can’t run various programming languages directly between different systems […]