需求就是把关卡内的代码稍作修改,将周杰伦前五页歌曲的歌词都爬取下来,结果就是全部展示打印出来。
#10 在QQ音乐中爬取某首歌曲的歌词
# 需求就是把关卡内的代码稍作修改,将周杰伦前五页歌曲的歌词都爬取下来,结果就是全部展示打印出来。
# 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 import requests
import html headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Origin': 'https://y.qq.com',
'Referer': 'https://y.qq.com/n/yqq/song/0039MnYb0qxYhV.html',
'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'
} for page in range(1,6): 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) search_html = res.json()
items = search_html['data']['song']['list']
for item in items:
item_id = item['id']
item_name = item['name'] 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)
item_html = item_res.json()
print('------------------------------------{}--------------------------------------'.format(item_name))
print(html.unescape(item_html['lyric'])) '''
执行结果如下:(上面设置了每页显示两首歌,查看前五页,也就是十首歌)
------------------------------------晴天--------------------------------------
[ti:晴天]
[ar:周杰伦]
[al:叶惠美]
[by:]
[offset:0]
[00:00.00]晴天 - 周杰伦 (Jay Chou)
------------------------------------告白气球--------------------------------------
[ti:告白气球]
[ar:周杰伦]
[al:周杰伦的床边故事]
[by:]
[offset:0]
[00:00.00]告白气球 - 周杰伦 (Jay Chou)
------------------------------------稻香--------------------------------------
[ti:稻香]
[ar:周杰伦]
[al:魔杰座]
[by:]
[offset:0]
[00:00.00]稻香 - 周杰伦 (Jay Chou)
------------------------------------青花瓷--------------------------------------
[ti:青花瓷]
[ar:周杰伦]
[al:我很忙]
[by:]
[offset:0]
[00:00.00]青花瓷 - 周杰伦 (Jay Chou)
------------------------------------等你下课(with 杨瑞代)--------------------------------------
[ti:等你下课(with 杨瑞代)]
[ar:周杰伦]
[al:]
[by:]
[offset:0]
[00:01.74]等你下课(with 杨瑞代) - 周杰伦 (Jay Chou)
------------------------------------说好的幸福呢--------------------------------------
[ti:说好的幸福呢]
[ar:周杰伦]
[al:闪亮2008 Hit Fm 年度百首单曲]
[by:]
[offset:0]
[00:00.00]说好的幸福呢 - 周杰伦 (Jay Chou)
------------------------------------龙卷风--------------------------------------
[ti:龙卷风]
[ar:周杰伦]
[al:Jay]
[by:]
[offset:0]
[00:00.00]龙卷风 - 周杰伦 (Jay Chou)
------------------------------------算什么男人--------------------------------------
[ti:算什么男人]
[ar:1]
[al:]
[by:]
[offset:0]
[00:00.00]算什么男人 - 周杰伦 (Jay Chou)
------------------------------------七里香--------------------------------------
[ti:七里香]
[ar:周杰伦]
[al:七里香]
[by:]
[offset:0]
[00:00.00]七里香 - 周杰伦 (Jay Chou)
------------------------------------简单爱--------------------------------------
[ti:简单爱]
[ar:周杰伦]
[al:范特西]
[by:]
[offset:0]
[00:00.00]简单爱 - 周杰伦 (Jay Chou)
'''