Skip to content

Instantly share code, notes, and snippets.

@bczhc
Last active March 24, 2022 14:54
Show Gist options
  • Save bczhc/a1a27e53aaacd22049441ad1d89be311 to your computer and use it in GitHub Desktop.
Save bczhc/a1a27e53aaacd22049441ad1d89be311 to your computer and use it in GitHub Desktop.
处理网易云音乐歌单中歌曲的评论
// 手动在页面控制台中获取歌单中的条目,并且保存成JSON以进行后面的处理
let table = document.getElementsByClassName("m-table")[0];
let r=Array.prototype.slice.call(table.getElementsByTagName("tbody")[0].children).map(x=>{
let a = x.getElementsByClassName("txt")[0].getElementsByTagName("a")[0];
let href = a.href;
let id = href.match(/^.*id=(.*)$/)[1];
let title = a.children[0].title;
return [title, id];
});
console.log(JSON.stringify(r));

然后用一些命令处理一下……

for id in `cat a.json | jq '.[][1]' --raw-output`; do echo "`curl "https://music.163.com/api/v1/resource/comments/R_SO_4_$id" | jq '.total'` $id"; done | tee output

输出的文件每行的格式为<评论数> <歌曲id>,有了这些之后就可以做sort操作。

有一个获取歌单的API: https://music.163.com/api/playlist/detail?id=3778678,但是好像是有问题的,有的时候会提示“服务器忙碌”这种消息。而且获取非自己的歌单的时候(获取自己的歌单也是要求账号登录的),只能获取出20条,对应着在网页版也是,会显示要下载客户端才能查看更多。

目前刚发现了一个办法,就是把目标歌单里的歌全都收藏到自己新建的一个歌单中,然后在网页版上打开属于自己账号里的那个临时歌单,就可以看到全部音乐了。这种方法依然只能手动做,并不是为爬虫的。

根据歌曲id查询歌曲名:

cat a.json | jq '.[] | select(.[1] == "1352818828") | .[0]' --raw-output

读取每行是<评论数> <歌曲id>格式的文本文件,然后评论数量升序排序,输出格式为<评论数> <歌曲名>

cat INPUT | sort -n | while read p; do echo "`echo "$p" | cut -d' ' -f1` $(cat INPUT-JSON | jq ".[] | select(.[1] == \"`echo "$p" | cut -d' ' -f2`\") | .[0]" --raw-output)"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment