Hand-on instructions for using Python to grab QQ music data (4th bounce)

[1. Project objectives]

By using Python to grab QQ music data (the first shot), we achieve the song name, album name, playback link of a song with a specified number of pages in the QQ music singer's single line.

By using Python to grab QQ music data (second round) by hand, we have achieved lyrics and top reviews for specified QQ music songs.

By teaching you how to grab QQ music data using Python (the third bomb), we have achieved more reviews and generated word clouds.

This time we will encapsulate three items together and control crawling of different data through the menu.

[2. libraries needed]

The libraries mainly involved are requests, openpyxl, html, json, wordcloud, jieba

A numpy library and a pipinstall pillow are also needed to change the background picture of a word cloud image

To generate.exe, pyinstaller -F is required

[3. Project Realization]

1. First determine what functions the menu will perform:

(1) Get song information (song title, album, link) of the designated singer

(2) Get the lyrics of specified songs

(3) Obtain comments on specified songs

(4) Generating Word Cloud Map

Exit the system

The code is as follows:

class QQ():

def menu(self):
    print('Welcome QQ Music crawler system, below is the function menu, please select.\n')
    while True:
            print('Function Menu\n1.Get song information for a designated singer\n2.Get the lyrics of the specified song\n3.Get specific song Reviews\n4.Generate Word Cloud Map\n5.Exit System\n')
            choice = int(input('Please enter the number to select the corresponding function:'))
            if choice == 1:
            elif choice == 2:
            elif choice == 3:
            elif choice == 4:
            elif choice == 5:
                print('Thanks for using!')
                print('Input error,Please re-enter.\n')
            print('Input error,Please re-enter.\n')  

The first line creates the class, and the second line defines the menu function. Instantiation of the class is used here. The first parameter of all functions is self. I think instantiation is more convenient for passing parameters.

whiletrue loops menus indefinitely;

Try...except...so that the loop does not exit with an error;

The other code opens different functions for setting different numbers to be entered.

2. Packaging item (1) get_info()

The code is as follows:

def get_info(self):

    #Create Workbook
    #Get workbook activity table
    #Sheet Rename

    sheet['A1'] ='Song Name'     #Add a header to assign values to A1 cells
    sheet['B1'] ='Owning album'   #Add a header to assign values to B1 cells
    sheet['C1'] ='Play Links'   #Add a header to assign values to C1 cells
    url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
    name = input('Please enter the name of the singer you want to query:')
    page = int(input('Please enter the number of song pages you need to query:'))
    for x in range(page):
        params = {
        'qqmusic_ver': '1298',
        res = requests.get(url,params=params)
        json = res.json()
        list = json['data']['song']['list']
        for music in list:
            song_name = music['name']
            # Find the song name and assign it to song_name
            album = music['album']['name']
            # Find the album name and assign it to album
            link = 'https://y.qq.com/n/yqq/song/' + str(music['mid']) + '.html\n\n'
            # Find playback links and assign links to links
            # Write name s, album s, and link s as lists, and Excel on multiple lines using the append function
    wb.save(name+'Before Personal Singles'+str(page*20)+'Detailed list.xlsx')            
    #Save and name this Excel file last
    print('Download successful!\n')

3. Encapsulation items (2) are get_id() and get_lyric

The code is as follows:

def get_id(self):

    self.i = input('Please enter the song name:')
    url_1 = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
    # This is the url requesting song comments
    headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    params = {'ct': '24', 'qqmusic_ver': '1298', 'new_json': '1', 'remoteplace': 'txt.yqq.song', 'searchid': '71600317520820180', 't': '0', 'aggr': '1', 'cr': '1', 'catZhida': '1', 'lossless': '0', 'flag_qc': '0', 'p': '1', 'n': '10', 'w': self.i, 'g_tk': '5381', 'loginUin': '0', 'hostUin': '0', 'format': 'json', 'inCharset': 'utf8', 'outCharset': 'utf-8', 'notice': '0', 'platform': 'yqq.json', 'needNewCode': '0'}  
    res_music = requests.get(url_1,headers=headers,params=params)
    json_music = res_music.json()
    self.id = json_music['data']['song']['list'][0]['id']
    # print(self.id)

def get_lyric(self):
    url_2 = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_yqq.fcg'
    # This is the url requesting song comments
    headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    params = {
    res_music = requests.get(url_2,headers=headers,params=params)
    js_1 = res_music.json()
    lyric = js_1['lyric']
    lyric_html = html.unescape(lyric)   #The escape character html.unescape method was used
    # print(lyric_html)
    f1 = open(self.i+'Lyric.txt','a',encoding='utf-8')    #Store in txt
    print('Download successful!\n')

In particular,'origin'and'referer' must be added to the headers of the downloaded lyrics, or the data should not be crawled down.

4. Packaging items (3) get_comment() and wordcloud()

The code is as follows:

def get_comment(self):

    page = input('Please enter the number of comment pages to download:')
    url_3 = 'https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg'
    headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    f2 = open(self.i+'comment.txt','a',encoding='utf-8')    #Store in txt
    for n in range(int(page)):
        params = {'g_tk_new_20200303': '5381', 'g_tk': '5381', 'loginUin': '0', 'hostUin': '0', 'format': 'json', 'inCharset': 'utf8', 'outCharset': 'GB2312', 'notice': '0', 'platform': 'yqq.json', 'needNewCode': '0', 'cid': '205360772', 'reqtype': '2', 'biztype': '1', 'topid': self.id, 'cmd': '6', 'needmusiccrit': '0', 'pagenum':n, 'pagesize': '15', 'lasthotcommentid':'', 'domain': 'qq.com', 'ct': '24', 'cv': '10101010'}
        res_music = requests.get(url_3,headers=headers,params=params)
        js_2 = res_music.json()
        comments = js_2['comment']['commentlist']
        for i in comments:
            comment = i['rootcommentcontent'] + '\n-----------------\n'
        # print(comment)
    print('Download successful!\n')

def wordcloud(self):
    self.name = input('Enter the name of the file you want to generate the word cloud:')
    def cut(text):
        space_wordlist=" ".join(wordlist_jieba)
        return space_wordlist
    with open(self.name+".txt" ,encoding="utf-8")as file:
        wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf",
        max_words= 100,
        wordcloud.to_file(self.name+'Cloud Word Map.png')  # Save the Word Cloud 
    print('Build succeeded!\n')

5. Instantiation of the last class

qq = QQ()
6. Effect display

Package into.exe
Packed with pyinstaller-F, runs with errors and flicks.

Looking at the image above, the error message should be related to the word cloud image. Comment out the library needed for the word cloud image. def wordcloud() can be packaged normally by modifying the image below, but it will not be able to generate the word cloud image anymore:

When you download lyrics or comments, if you have a song with a duplicate name, you can add the name of the singer to the front of the song, such as "Dun Purple Bubble" in the picture above.

[4. Summary]

1. Item 4 reviews the first three items, consolidating the knowledge points of crawlers and reviewing the related usage of classes at the same time.

2. The first three items can be self-stamped; the article will learn: Hand-by-hand teaches you to use Python to grab QQ music data (first shot), Hand-by-Hand teaches you to use Python to grab QQ music data (second shot), Hand-by-Hand teaches you to use Python to grab QQ music data (third shot).

3. Thank you for watching. It's not easy to write more than a hundred lines of code.Wishing your little partners success in their studies and smooth work!

4. If you need the source code of this article, please reply to the four words "QQ Music" in the background of public number to get it. It feels good, remember to give a star oh.

What do you gain from reading this article?Forward to more people

IT Shared Home

Please reply in the background of WeChat to join the group

Keywords: Python JSON Windows encoding

Added by Stoned Gecko on Sun, 26 Apr 2020 20:29:49 +0300