需求就是把關卡內的程式碼稍作修改,將周杰倫前五頁歌曲的歌詞都爬取下來,結果就是全部展示列印出來。
1 #10 在QQ音樂中爬取某首歌曲的歌詞 2 # 需求就是把關卡內的程式碼稍作修改,將周杰倫前五頁歌曲的歌詞都爬取下來,結果就是全部展示列印出來。 3 # URL https://y.qq.com/portal/search.html#page=1&searchid=1&remoteplace=txt.yqq.top&t=song&w=%E5%91%A8%E6%9D%B0%E4%BC%A6 4 5 import requests 6 import html 7 8 headers = { 9 'Accept': 'application/json, text/javascript, */*; q=0.01', 10 'Origin': 'https://y.qq.com', 11 'Referer': 'https://y.qq.com/n/yqq/song/0039MnYb0qxYhV.html', 12 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' 13 } 14 15 for page in range(1,6): 16 17 res = requests.get('https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=67198573060150304&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p={}&n=2&w=%E5%91%A8%E6%9D%B0%E4%BC%A6&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0'.format(page),headers=headers) 18 19 search_html = res.json() 20 items = search_html['data']['song']['list'] 21 for item in items: 22 item_id = item['id'] 23 item_name = item['name'] 24 25 item_res = requests.get('https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_yqq.fcg?nobase64=1&musicid={}&-=jsonp1&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0'.format(item_id),headers=headers) 26 item_html = item_res.json() 27 print('------------------------------------{}--------------------------------------'.format(item_name)) 28 print(html.unescape(item_html['lyric'])) 29 30 ''' 31 執行結果如下:(上面設定了每頁顯示兩首歌,檢視前五頁,也就是十首歌) 32 ------------------------------------晴天-------------------------------------- 33 [ti:晴天] 34 [ar:周杰倫] 35 [al:葉惠美] 36 [by:] 37 [offset:0] 38 [00:00.00]晴天 - 周杰倫 (Jay Chou) 39 ------------------------------------告白氣球-------------------------------------- 40 [ti:告白氣球] 41 [ar:周杰倫] 42 [al:周杰倫的床邊故事] 43 [by:] 44 [offset:0] 45 [00:00.00]告白氣球 - 周杰倫 (Jay Chou) 46 ------------------------------------稻香-------------------------------------- 47 [ti:稻香] 48 [ar:周杰倫] 49 [al:魔傑座] 50 [by:] 51 [offset:0] 52 [00:00.00]稻香 - 周杰倫 (Jay Chou) 53 ------------------------------------青花瓷-------------------------------------- 54 [ti:青花瓷] 55 [ar:周杰倫] 56 [al:我很忙] 57 [by:] 58 [offset:0] 59 [00:00.00]青花瓷 - 周杰倫 (Jay Chou) 60 ------------------------------------等你下課(with 楊瑞代)-------------------------------------- 61 [ti:等你下課(with 楊瑞代)] 62 [ar:周杰倫] 63 [al:] 64 [by:] 65 [offset:0] 66 [00:01.74]等你下課(with 楊瑞代) - 周杰倫 (Jay Chou) 67 ------------------------------------說好的幸福呢-------------------------------------- 68 [ti:說好的幸福呢] 69 [ar:周杰倫] 70 [al:閃亮2008 Hit Fm 年度百首單曲] 71 [by:] 72 [offset:0] 73 [00:00.00]說好的幸福呢 - 周杰倫 (Jay Chou) 74 ------------------------------------龍捲風-------------------------------------- 75 [ti:龍捲風] 76 [ar:周杰倫] 77 [al:Jay] 78 [by:] 79 [offset:0] 80 [00:00.00]龍捲風 - 周杰倫 (Jay Chou) 81 ------------------------------------算什麼男人-------------------------------------- 82 [ti:算什麼男人] 83 [ar:1] 84 [al:] 85 [by:] 86 [offset:0] 87 [00:00.00]算什麼男人 - 周杰倫 (Jay Chou) 88 ------------------------------------七里香-------------------------------------- 89 [ti:七里香] 90 [ar:周杰倫] 91 [al:七里香] 92 [by:] 93 [offset:0] 94 [00:00.00]七里香 - 周杰倫 (Jay Chou) 95 ------------------------------------簡單愛-------------------------------------- 96 [ti:簡單愛] 97 [ar:周杰倫] 98 [al:范特西] 99 [by:] 100 [offset:0] 101 [00:00.00]簡單愛 - 周杰倫 (Jay Chou) 102 '''