一、题目:
将周杰伦前五页歌曲的歌词都爬取下来,结果就是全部展示打印出来。
我的答案:
import requests
url = "https://c.y.qq.com/soso/fcgi-bin/client_search_cp"
for i in range(1,6):
parpams = {
"ct": '24',
'qqmusic_ver': "1298",
'remoteplace': 'txt.yqq.lyric',
'searchid': '95697768404081207',
'aggr': '0',
'catZhida': '1',
'lossless': '0',
'sem': '1',
't': '7',
'p': str(i),
'n': '5',
'w': '周杰伦',
'g_tk_new_20200303': '5381',
'g_tk': '5381',
'loginUin': '0',
'hostUin': '0',
'format': 'json',
'inCharset': 'utf8',
'outCharset': 'utf-8',
'notice': '0',
'platform': 'yqq.json',
'needNewCode': '0'
}
res_word = requests.get(url, params=parpams)
json_word = res_word.json()
#jsonres = json_word.loads(json_word.text)
# 或者使用json来解析res.text成列表
list_word = json_word['data']['lyric']['list']
for word in list_word:
print(word["content"])
print("-----------------------------------------")
二、题目
输入喜欢的歌手,就可以查到其对应的专辑、歌名、链接;
import requests
url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
singer = input("输入您想要查的歌手:")
for x in range(2):
params = {
'ct':'24',
'qqmusic_ver': '1298',
'new_json':'1',
'remoteplace':'txt.yqq.song',
'searchid':'70717568573156220',
't':'0',
'aggr':'1',
'cr':'1',
'catZhida':'1',
'lossless':'0',
'flag_qc':'0',
'p':str(x+1),
'n':'20',
'w': singer,
'g_tk':'714057807',
'loginUin':'0',
'hostUin':'0',
'format':'json',
'inCharset':'utf8',
'outCharset':'utf-8',
'notice':'0',
'platform':'yqq.json',
'needNewCode':'0'
}
# 将参数封装为字典
res_music = requests.get(url,params=params)
# 调用get方法,下载这个列表
json_music = res_music.json()
# 使用json()方法,将response对象,转为列表/字典
list_music = json_music['data']['song']['list']
# 一层一层地取字典,获取歌单列表
for music in list_music:
# list_music是一个列表,music是它里面的元素
print(music['name'])
# 以name为键,查找歌曲名
print('所属专辑:'+music['album']['name'])
# 查找专辑名
print('播放时长:'+str(music['interval'])+'秒')
# 查找播放时长
print('播放链接:https://y.qq.com/n/yqq/song/'+music['mid']+'.html\n\n')
# 查找播放链接
三、题目
实现功能:用户输入快递名称和单号,程序即可在快递100https://www.kuaidi100.com/爬取最新物流状态,并将其打印出来。
import requests
url = "https://www.kuaidi100.com/query"
logisticsName = input('你的快递是什么物流呀?')
courierNum = input('你的快递单号是什么呀?')
params = {
'type': logisticsName,
'postid': courierNum,
'temp': '0.5296744539482263',
'phone':''
}
res = requests.get(url,params=params)
# result = res.json()
# print(result['data'][0]['context'])
results = res.json()["data"]
for line in results:
time = line['time']
add = line["context"]
print(time,"\n",add,"\n")
print("-------------------------------------")
代码有问题,temp号无法确定,输入网页上的号码,出来的结果也不一样;
网友评论