golang+es爬取网易云音乐评论

本文介绍了使用Golang语言构建一个爬虫,目标是抓取网易云音乐的评论信息,存储到MySQL或Elasticsearch中。作者首先详细描述了爬取评论、歌曲列表和下载歌曲的结构设计,包括评论struct、歌曲列表struct和歌曲下载struct。此外,还提到了加密过程,特别是解密网易云音乐评论的关键步骤,涉及到AES加密。整个项目采用Goquery和正则表达式进行网页解析,并利用命令行工具初始化数据库连接。
摘要由CSDN通过智能技术生成
目的和准备
目的:

为了知道自己歌单中每首歌的评论,然后通过歌曲id来检索这首歌的所有评论,并且想熟悉运用golang中的channel以及整个爬虫架构

准备:

语言:golang

编辑器: goland

处理:goquery/正则

存储:mysql(elasticsearch)

V1版本(单机版,mysql存储)

models
评论struct[comment.go]

首先通过查看某一首音乐的请求获取到 ajax(eg:https://music.163.com/weapi/v1/resource/comments/R_SO_4_340394?csrf_token=) 请求的json 通过返回的json数据构造出struct

//每首歌的评论
type Commentt struct {
	Id         int64
	IsMusician bool  `json:"isMusician"`
	UserId     int32 `json:"userId"`
	//TopComments []string       `json:"topComments";gorm:"-"`
	MoreHot     bool           `json:"moreHot"`
	HotComments []*HotComments `json:"hotComments"`
	Code        int            `json:"code"`
	Comments    []*Comments    `json:"comments"`
	Total       int64          `json:"total"`
	More        bool           `json:"more"`
	SongId      string
}



//具体到每条评论(普通评论)
type Comments struct {
	Id                 int64
	User               *User        `json:"user"`
	BeReplied          []*BeReplied `json:"-"`
	Time               int64        `json:"time"`
	LikedCount         int          `json:"likedCount"`
	Liked              bool         `json:"liked"`
	CommentId          int64        `json:"commentId"`
	Content            string       `json:"content";gorm:"type:longtext"`
	IsRemoveHotComment bool         `json:"isRemoveHotComment"`
	Commentt           *Commentt
	CommenttID         int64
}

//热门评论
type HotComments struct {
	Id         int64
	User       *User        `json:"user"`
	BeReplied  []*BeReplied `json:"-"`
	Time       int64        `json:"time"`
	LikedCount int          `json:"likedCount"`
	Liked      bool         `json:"liked"`
	CommentId  int64        `json:"commentId"`
	Content    string       `json:"content";gorm:"type:longtext"`
	Commentt   *Commentt
	CommenttID int64
}

//评论的用户信息
type User struct {
	Id           int64
	LocationInfo *LocationInfo `json:"-"`
	UserType     int           `json:"userType"`
	ExpertTags   *ExpertTag    `json:"-"`
	UserId       int64         `json:"userId"`
	NickName     string        `json:"nickName"`
	Experts      *Expert       `json:"-"`
	AuthStatus   int           `json:"authStatus"`
	RemarkName   *RemarkName   `json:"-"`
	AvatarUrl    string        `json:"avatarUrl"`
	VipType      int           `json:"vipType"`
	Comments     *Comments
	CommentsID   int64
}


//答复用户的评论信息
type BeReplied struct {
	Id            int64
	User          *User  `json:"-"`
	UserID        int64
	Content       string `json:"content"`
	Status        int    `json:"status"`
	CommentsID    int64
	HotCommentsID int64
}

type LocationInfo struct {
}

type ExpertTag struct {
}

type Expert struct {
}

type RemarkName struct {
}


然后又查看了歌单信息看一了下网页Dom结构构造出如下struct

歌曲列表struct[songList.go]


type Song struct {
	Id          int64
	SongUrlId   string
	SongId      string
	Title       string
	DownloadUrl string
	UserId      string
}

type PlayList struct {
	Id     int64
	UserId string
	Songs  []Song
}

然后又找了一下歌曲下载的接口发现只能在pc端下载,于是在一篇博客中找到了一些关于golang爬取网易云音乐的具体实现点击此处

歌曲下载struct[download.go]
type DownloadData struct {
	Data []*Data `json:"data"`
	Code int     `json:"code"`
}

type Data struct {
	Id        int64   `json:"id"`
	Url       string  `json:"url"`
	Br        int64   `json:"br"`
	Md5       string  `json:"md_5"`
	Code      int     `json:"code"`
	Expi      int     `json:"expi"`
	Type      string  `json:"type"`
	Gain      float64 `json:"gain"`
	Fee       int     `json:"fee"`
	Uf        *Uf     `json:"-"`
	Payed     int     `json:"payed"`
	Flag      int     `json:"flag"`
	CanExtend bool    `json:"can_extend"`
}

type Uf struct {
}


推荐一个很好用的jsonstruct工具

以上就是所有数据库表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值