python爬虫第5关项目歌词爬虫

需求就是把关卡内的代码稍作修改,将周杰伦前五页歌曲的歌词都爬取下来,结果就是全部展示打印出来。
URL:https://y.qq.com/portal/search.html#page=1&searchid=1&remoteplace=txt.yqq.top&t=lyric&w=%E5%91%A8%E6%9D%B0%E4%BC%A6
python爬虫第5关项目歌词爬虫_第1张图片

import requests

singer=input('你想查询哪位歌手的名字?')
for i in range(1,6):
        res=requests.get('https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&remoteplace=txt.yqq.lyric&searchid=91763561204056573&aggr=0&catZhida=1&lossless=0&sem=1&t=7&p={}&n=5&w=%E5%91%A8%E6%9D%B0%E4%BC%A6&g_tk=1676601595&loginUin=835926124&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0'.format(i))
        html=res.json()
        contents=html['data']['lyric']['list']
        for content in contents:
                print(content['content'].replace('\\n','\n').replace('','').replace('',''))#\\n表示真的\n字符而不是换行符,将歌词中的\n替换成换行符。
                print('---------------------------------')
                

你可能感兴趣的:(python基础及爬虫)