Python is short-crawling music

python bitter short-download music#

Overview##

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

Software goals##

  1. Develop a music downloader
  2. Get music through crawlers to analyze music on the Internet
  3. Realize the interface to download music

The tutorial comes from python learners at station B

Code details##

# 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()

Program running results##

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

Concluding remarks##

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

Python is short-crawling music
Python 3.9 is here!
Why python is popular
Python is slowly fading
Python play pause music
Is python an interpreted language?
Is python an interpreted language
Python is short-world epidemic map
Python is short _SVM test
Is python code case sensitive
What is introspection in python
What is object-oriented in python
What is Python variable scope
What is list comprehension in python
Is there function overloading in python
Python deep copy is not perfect
Python judges that it is empty
Python is a cross-platform language code
What is the use of Python
Is python language free or paid?
Everything in Python is an object
Is python suitable for data mining
Is python crawler easy to learn
Is there an algorithm in python language
What is the scope of python variables
What is the id function of python
What is an anonymous function in Python
Where is the pip path of python3
What system is good for self-study python
What is the prospect of python development
What is a sequence table in Python
Is a number in python a variable type
Python example method to open music files
Why is python suitable for writing crawlers
alert! Python is slowly losing its charm!
What is the function body of python
Python is mainly used in which directions
Is there a helper function in python
Where is the directory after python installation
How to add background music in python