10、 在QQ音乐中爬取某首歌曲的歌词
需求就是把关卡内的代码稍作修改,将周杰伦前五页歌曲的歌词都爬取下来,结果就是全部展示打印出来。
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 \'\'\'