鱼C论坛

 找回密码
 立即注册
查看: 819|回复: 5

[已解决]爬歌词

[复制链接]
发表于 2020-6-10 22:10:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 jump_p 于 2020-6-10 22:23 编辑
  1. import requests
  2. import json


  3. def get_song_lyric(res):
  4.     lyric_json = json.loads(res.text)
  5.     with open('lyric.txt', 'w', encoding='utf-8') as file:
  6.         lyric_json['lrc']['lyric']
  7.         
  8.             


  9. def get_lyric(url):
  10.     headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}

  11.     params = "省略"
  12.     encSecKey = "省略"

  13.     data = {"params": params,
  14.             "encSecKey": encSecKey}

  15.     name_id = url.split('=')[1]
  16.     target_url = "http://music.163.com/api/song/lyric?id={}&lv=1&kv=1&tv=-1".format(name_id)

  17.     res = requests.post(target_url, headers=headers, data=data)

  18.     return res


  19. def main():
  20.     url = input("请输入链接地址:")
  21.     res = get_lyric(url)
  22.     get_song_lyric(res)


  23. if __name__ == "__main__":
  24.     main()
复制代码



模仿小甲鱼爬网易云音乐评论写的,帮忙补一下get_song_lyric(res),这一段不知道怎么写了,返回的json是个字典
最佳答案
2020-6-10 22:49:52
  1. import requests
  2. import json


  3. def get_hot_comments(res):
  4.     lyric_json = json.loads(res.text)
  5.     content = json.loads(res.text)['lrc']['lyric']
  6.     with open('lyric.txt', 'w', encoding='utf-8') as file:
  7.         file.write(content)

  8. def get_lyric(url):
  9.     headers = {
  10.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}

  11.     params = ""
  12.     encSecKey = ""

  13.     data = {"params": params,
  14.             "encSecKey": encSecKey}

  15.     name_id = url.split('=')[1]
  16.     target_url = "http://music.163.com/api/song/lyric?id={}&lv=1&kv=1&tv=-1".format(name_id)

  17.     res = requests.post(target_url, headers=headers, data=data)

  18.     return res


  19. def main():
  20.     url = input("请输入链接地址:")
  21.     res = get_lyric(url)
  22.     get_hot_comments(res)


  23. if __name__ == "__main__":
  24.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-10 22:49:52 | 显示全部楼层    本楼为最佳答案   
  1. import requests
  2. import json


  3. def get_hot_comments(res):
  4.     lyric_json = json.loads(res.text)
  5.     content = json.loads(res.text)['lrc']['lyric']
  6.     with open('lyric.txt', 'w', encoding='utf-8') as file:
  7.         file.write(content)

  8. def get_lyric(url):
  9.     headers = {
  10.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}

  11.     params = ""
  12.     encSecKey = ""

  13.     data = {"params": params,
  14.             "encSecKey": encSecKey}

  15.     name_id = url.split('=')[1]
  16.     target_url = "http://music.163.com/api/song/lyric?id={}&lv=1&kv=1&tv=-1".format(name_id)

  17.     res = requests.post(target_url, headers=headers, data=data)

  18.     return res


  19. def main():
  20.     url = input("请输入链接地址:")
  21.     res = get_lyric(url)
  22.     get_hot_comments(res)


  23. if __name__ == "__main__":
  24.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-6-10 22:55:05 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-10 22:59:37 | 显示全部楼层


  话说你互助团队怎么这么快到账  哈哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-10 23:00:48 | 显示全部楼层
Twilight6 发表于 2020-6-10 22:59
话说你互助团队怎么这么快到账  哈哈哈

昨天540,今天608,你过分了啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-10 23:01:44 | 显示全部楼层
Stubborn 发表于 2020-6-10 23:00
昨天540,今天608,你过分了啊

  没有 这两天才涨 40 个  你记错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 18:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表