Libraries used
requests:
Request library used by the crawler
jsonpath:
A third-party library for processing json data
os:
Python file system operation library
urlretrieve:
Url parsing library
tkinter:
Tkinter is a GUI library that is used more in python. GUI refers to the control operation of the page. It is similar to the most basic editor R-gui in R. It provides some of the most basic drawing tools, which can realize graphical buttons, text boxes, A bit of VB flavor, the graphic controls used in this article mainly include
The tutorial comes from python learners at station B
# Import the required libraries
# Search song name
import requests
import jsonpath
import os
from urllib.request import urlretrieve
from tkinter import*
# Crawler part=-===============================================
# Song download function
def song_load(url,title):
# Create a folder to save downloaded music
os.makedirs("music",exist_ok=TRUE)
# Set save path and save name
path ="music\{}.mp3".format(title)
# Insert text to show download progress
text.insert(END,'song:{}...downloading...'.format(title))
# Text box scroll
text.see(END)
# Update
text.update()
# Request download via urlretrieve function
urlretrieve(url,path)
# Show the download is complete in the text control
text.insert(END,'{}Has been downloaded'.format(title))
# Text box scroll
text.see(END)
# Update
text.update()
# Define music acquisition function
def get_music_name():
name=entry.get()
# Add request header to prevent anti-scrabble
# This is different from the previous one using X-Requested-With
headers={'X-Requested-With':'XMLHttpRequest'}
# The parameters are the special requirements of the post request, such as login and search, etc.
# At this time, you need to add the form data to the request function
params={'input':name,'filter':'name','type':'netease','page':1,}
# The destination URL is a free vip music analysis website
url ='http://www.youtap.xin/'
# Request the destination URL through the post function
resp = requests.post(url,data=params,headers=headers)
# Parse the target through the json function
data = resp.json()
# Get the first name of the target music
title=jsonpath.jsonpath(data,"$..title")[0]
# Get target music author
author=jsonpath.jsonpath(data,"$..author")[0]
# Get the download address
url=jsonpath.jsonpath(data,"$..url")[0]
# download music
song_load(url,title)
# Interface layout-==========================================================
# 1. Create a canvas and build controls through Tk functions
root =Tk()
# 2. Add control title
root.title("Music downloader")
# 3. Set the window size of the program
root.geometry('760x550+400+200')
# 4. Label component
label=Label(root,text='Please enter the downloaded song: ',font=('Chinese Xingkai',20))
# 5. Label positioning
label.grid()
# 6. Set the input box and position the input box as 0 row and 1 column
entry =Entry(root,font=('Official script',20))
entry.grid(row=0, column=1)
# 7. Set the list box and position it as 1 row 0-2 columns, set the cell size at the same time
text=Listbox(root,font=('Official script',20),width=50,height=15)
text.grid(row=1,columnspan=2)
# 8. Add download button control
# Call the previously written get through the command parameter_music_name function
button1=Button(root,text='download: ',font=('Official script',15),command=get_music_name)
button1.grid(row=2,column=0,sticky=W)
# 9. Exit button
# Call the exit function of the system through the command parameter to achieve exit
button2=Button(root,text='drop out: ',font=('Official script',15),command=root.quit)
button2.grid(row=2,column=1,sticky=E)
# Display the interface continuously through the loop, otherwise the interface will flash by
root.mainloop()
interface
The interface is not perfect, mainly due to the size setting of the interface, there are many useless borders
To download
Input boxes, download buttons, exit buttons, and list box controls work well
Download results
In the working directory, find the downloaded music
Test audible
Through the music player test, the song is complete
It’s been almost a week since school started today. After returning to Beijing, I have been busy. I went to Shandong on a business trip halfway through. I also dragged the official account of Rigeng into Zhougeng. There have been a lot of things recently. May the world be peaceful, people People are happy.
love&peace
Recommended Posts