简易版本以CSV形式呈现爬取结果,完整代码如下:
import requests # 请求
from fake_useragent import UserAgent
import json
print("请输入您想要爬取清单的歌手名:")
name = input()
ua = UserAgent()
headers = {
'User-Agent': ua.random,
'cookie':'RK=LdWlHMsQ+b; ptcz=42785168e679b66b7913e09a4387fc94c5ad2d81419840eb33a502abc14ae6c6; pgv_pvid=4366402929; fqm_pvqid=ed1a5c76-5778-4d72-aa4f-389d94cd126e; ts_uid=886687551; fqm_sessionid=2b4a4a2f-b921-4e70-861d-54a608695f10; pgv_info=ssid=s5047316408; ts_refer=www.so.com/link; _qpsvr_localtk=0.49574447171587144; login_type=1; wxopenid=; tmeLoginType=2; psrf_qqaccess_token=D40E8A445E33FC38FB47291B44C03E96; qqmusic_key=Q_H_L_5Opuh_YbF8NbIlG-FqC_2ns2gXyWSTh_cplWyZPhEpyIWDVtQUGLwQQ; psrf_access_token_expiresAt=1656146941; psrf_qqunionid=93ABF9072A8734C330E108787CC182AE; uin=2363310076; wxunionid=; qm_keyst=Q_H_L_5Opuh_YbF8NbIlG-FqC_2ns2gXyWSTh_cplWyZPhEpyIWDVtQUGLwQQ; psrf_musickey_createtime=1648370941; qm_keyst=Q_H_L_5Opuh_YbF8NbIlG-FqC_2ns2gXyWSTh_cplWyZPhEpyIWDVtQUGLwQQ; psrf_qqopenid=900C2C2A46F36818FEB00C24A5EEC6B0; wxrefresh_token=; psrf_qqrefresh_token=8289BF671C8907272471F03D564F5A69; euin=owosoio5oenl7c**; ts_last=y.qq.com/n/ryqq/search',
'referer':'https://y.qq.com/'
}
url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?p=1&n=10&w={}'
resp = requests.get(url.format(name), headers=headers)
json_str = resp.text
json_str = json_str[9:-1]
json_dict = json.loads(json_str)
songlist = json_dict['data']['song']['list']
f = open("list.csv", mode="w", encoding="utf-8")
for song in songlist:
print('歌名或专辑名为:'+song['albumname'])
f.write(song['albumname'])
f.write('\n')
2.6.通过代码进行演示运行结果
以周深为例: