爬虫入门—获取网易云音乐歌曲列表

注:爬取网易云歌手数据,仅供学习与交流,无意侵犯。

因为近期文章末尾追加了网易云的单曲播放,心血来潮,想获取流行歌手的热门歌曲和网址链接,以便后期省去搜索单曲的冗余。“群众的眼睛是雪亮的”,一般而言,大凡歌手的前几名热门歌曲都是经得起群众的考验的,我随手一取,便是金曲。于是有了这篇爬虫入门文章。

进入网易云音乐网站首页,获取歌手链接。以歌手链接http://music.163.com/discover/artist 为种子,层次遍历,以歌手分类->歌手列表->歌手歌曲递进,层层深入,获取所有歌手歌曲链接。

music163_index

http://music.163.com/discover/artist/ 进入歌手分类页,这里主要爬取【华语】和【欧美】的歌手歌曲。日本和韩国的歌曲就不爬了吧。主要还是要分析网页的结构化数据,以css选择器的方式获取链接数据。

music163_artist

通过分析可以得到这样的选择器 .g-sd2 .blk a 从而获取歌手分类链接。

进入歌手列表页后,发现歌手以字典序来排,其中热门链接页会以字典序页重复,记得排重处理。

music163_artistcatary

通过分析,也可以得到这样的选择器 .g-wrap .n-ltlst.f-cb a,从而获取到一个类的歌手列表。

进入类歌手列表后,要分析出歌手的歌曲列表链接。

music163_artistcat2

通过分析,也可以得到这样的选择器 .m-sgerlist .nm.nm-icn.f-thide.s-fc0 ,从而获取到歌手的歌曲列表链接。

进入歌手的歌曲列表页后,发现歌曲列表是以table表格页显示的,比较麻烦,但通过分析网页源码发现,竟然隐藏了一个歌曲列表,大赞。

music163_artistsong

通过分析,也可以得到这样的选择器 ul.f-hide a,从而就可以获取歌曲名和相应的歌曲链接。而这个歌曲链接就可以作为本站的网易云的外链播放器播放啦。

在爬取分析的时候,发现网易云音乐并没有做严格的反爬机制,代理池,限频延时,自己看着办。本次采用Java语言编写,利用httpclient、jsoup等jar包爬取分析,获取歌曲链接后存储到Excel,以歌手名为表头,对应的列为相应的歌曲链接和歌曲名。遇到js生成网页的部分,可能要用到浏览器模拟工具或者自动化测试工具,如httpunit、selenium等等。效果图如下,

music163_result

下面是截取的一些maven依赖:

  1. <!– https://mvnrepository.com/artifact/com.google.code.gson/gson –>  
  2.     <dependency>  
  3.         <groupId>com.google.code.gson</groupId>  
  4.         <artifactId>gson</artifactId>  
  5.         <version>2.7</version>  
  6.     </dependency>  
  7.       
  8.     <!–      
  9.     <dependency>    
  10.         <groupId>commons-httpclient</groupId>    
  11.         <artifactId>commons-httpclient</artifactId>    
  12.         <version>3.1</version>    
  13.     </dependency>    
  14.     —>    
  15.     <!– https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient –>  
  16.     <dependency>  
  17.         <groupId>org.apache.httpcomponents</groupId>  
  18.         <artifactId>httpclient</artifactId>  
  19.         <version>4.5.3</version>  
  20.     </dependency>  
  21.   <!– https://mvnrepository.com/artifact/org.httpunit/httpunit –>  
  22.     <dependency>  
  23.         <groupId>org.httpunit</groupId>  
  24.         <artifactId>httpunit</artifactId>  
  25.         <version>1.7.2</version>  
  26.     </dependency>  
  27.     
  28.     <!– https://mvnrepository.com/artifact/org.jsoup/jsoup –>  
  29.     <dependency>  
  30.         <groupId>org.jsoup</groupId>  
  31.         <artifactId>jsoup</artifactId>  
  32.         <version>1.10.2</version>  
  33.     </dependency>  
  34.       
  35.     <!– https://mvnrepository.com/artifact/commons-logging/commons-logging –>  
  36.     <dependency>  
  37.         <groupId>commons-logging</groupId>  
  38.         <artifactId>commons-logging</artifactId>  
  39.         <version>1.2</version>  
  40.     </dependency>  
  41.     
  42.     <!– https://mvnrepository.com/artifact/commons-codec/commons-codec –>  
  43.     <dependency>  
  44.         <groupId>commons-codec</groupId>  
  45.         <artifactId>commons-codec</artifactId>  
  46.         <version>1.10</version>  
  47.     </dependency>  
  48.     
  49.     <!– https://mvnrepository.com/artifact/log4j/log4j –>  
  50.     <dependency>  
  51.         <groupId>log4j</groupId>  
  52.         <artifactId>log4j</artifactId>  
  53.         <version>1.2.17</version>  
  54.     </dependency>  
  55.    
  56.      <!– https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml –>  
  57.     <dependency>  
  58.         <groupId>org.apache.poi</groupId>  
  59.         <artifactId>poi-ooxml</artifactId>  
  60.         <version>3.16</version>  
  61.     </dependency>  

入门都是简单的,难能可贵的是坚持。发现自己爱打游击战了,没有一点方向。。。

参考

http://hc.apache.org/ httpclient

https://jsoup.org/ jsoup java版网页解析器

http://www.httpunit.org/ httpunit

http://www.seleniumhq.org/ selenium


发表评论

电子邮件地址不会被公开。 必填项已用*标注

17 + 25 = ?