In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Python to download music on a certain sound at one time". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use Python to download music on a certain sound at once".
Python Link Douyin
Python downloads some audio content posts online, but all of them are troublesome and need to be simulated after connecting to an Android phone through adb. I'm too lazy to play with this kind of thing. So, how to get a certain audio content? There are probably two ways to search the Internet, one is the browser plug-in Quick Shake, and the other is the audio web version I'm going to talk about today. In fact, the difference between the two is not very big, first download some audio content to the server, through the development of a simple website to configure the domain name, for everyone to visit.
Crawler implements to analyze the contents of the hot song list.
Let's first take a look at this Douyin hot song list of 20 songs per page, one on 55 pages. But if you are not careful, you can find that many songs have the problem of repetition. So, when we wait for the crawler, we need to prepare a music_list to identify whether the song has been downloaded or not.
Web page parsing
The web page is relatively simple. A ul > li*20 is wrapped in a div. Should we get it like this:
Soup.find ('div', {"class": "pull-left"}). Find (' ul'). FindAll ('a')
If you say yes, then you must not have read the article I sorted out the day before yesterday through which Douban movie review will take you to analyze python crawler Quick start: https://www.jianshu.com/p/ae38f7607902, I specifically mentioned a tip in the article, by using the attributes of attr for quick parsing, then the fastest way to get it is:
Soup.findAll ('asides, attrs= {' onclick': True})
We just need to get all the a tags and cut the tags that contain the attribute onclick.
Skillful use of eval
We parsed the content through attr ['onclick'], we can get its attribute open1 (' Night', 'http://p9-dy.byteimg.com/obj/61a20007a98954b0831d)'). How can we quickly get the song name and url? here we need to use an eval trick:
Index = "open1 ('night', 'http://p9-dy.byteimg.com/obj/61a20007a98954b0831d','')"index[5:]"(' night', 'http://p9-dy.byteimg.com/obj/61a20007a98954b0831d','')"index_tuple = eval (index [5:]) print (index_tuple, type (index_tuple) (' night', 'http://p9-dy.byteimg.com/obj/61a20007a98954b0831d',' '') index_tuple [0] 'Night' index_tuple [1] 'http://p9-dy.byteimg.com/obj/61a20007a98954b0831d'
Ps: today, a friend said that I wrote the code without comments. I'm telling you how to write code that others can't understand at all, that is, not to write comments, ! In fact, I explained the code bit by bit in the article, so I didn't write it, but with the mentality of being afraid of the bosses, I'd better add the comments.
Code implementation
Generally speaking, the implementation is relatively simple, and the whole code is as follows:
Import osimport requestsfrom bs4 import BeautifulSoupimport threadingimport timeclass DouYinMusic: def _ _ init__ (self): self.music_list = [] self.path = self.download_path () @ staticmethod def download_path (): "get the code execution directory And create a Music folder under the directory: return Music folder full path "" base_dir = os.path.dirname (os.path.abspath (_ _ file__)) _ path = os.path.join (base_dir, "Music") if not os.path.exists (_ path): os.mkdir (_ path) return _ path def get_request (self Url): "encapsulates the requests.get method if it is requested for a web page Return the content of the web page. Otherwise, parse the music address. And return music binaries: param url: request url (divided into web pages and music): return: webpage content & music binaries "" r = requests.get (url, timeout=5) if url.endswith ('html'): return r.text else: return r.content def analysis_html (self) Html): based on the content of the acquired web page Parse the music name and download address and call the music download method: param html: "" soup = BeautifulSoup (html, 'lxml') # find each download address for tag_a in soup.findAll (' a download, attrs= {'onclick': True}): # download format' ("name", "link", "") 'according to the keyword onclick Convert str to tuple type link_list = eval (tag_a ['onclick'] [5:]) music_name through eval, music_link = link_list [: 2] # because there is partial repetition of music Therefore, set to judge that downloaded music skips if music_name in self.music_list: continue self.music_list.append (music_name) t = threading.Thread (target=self.download_music, args= (music_name, music_link)) time.sleep (0.5) t.start () def download_music (self, music_name) Music_link): "" parse the music file and complete the music download: param music_name: music name: param music_link: download address "" _ full_name = os.path.join (self.path, music_name) with open (_ full_name + '.mp3') 'wb') as f: f.write (self.get_request (music_link)) print ("Douyin Music: {} download completed" .format (music_name)) def run (self): "main method For batch generation of url "" for page in range (1Power55): url = "http://douyin.bm8.com.cn/t_{}.html".format(page) html = self.get_request (url) self.analysis_html (html) if _ _ name__ ='_ _ main__': main = DouYinMusic () main.run () so far I believe that everyone has a deeper understanding of "how to use Python to download music on a certain sound at one time". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.