@@ -28,12 +28,22 @@ def sql(self):
2828 alium VARCHAR NOT NULL,
2929 lyric VARCHAR NOT NULL
3030 );
31+
32+ CREATE TABLE IF NOT EXISTS comment_text(
33+ song_id VARCHAR,
34+ userId VARCHAR NOT NULL,
35+ nickname VARCHAR NOT NULL,
36+ comment TEXT NOT NULL,
37+ FOREIGN KEY (song_id) REFERENCES song_information (song_id)
38+ );
3139 """
32- self .cursor .execute (sql )
40+ self .cursor .executescript (sql )
3341 self .cursor .close ()
3442 self .connent_sql .commit ()
3543 self .connent_sql .close ()
36- def sava_sql (self ,song_id ,singer ,song_name ,alium ,lyric ):
44+
45+ def song_sql (self ,song_id ,singer ,song_name ,alium ,lyric ):
46+ self .sql ()
3747 self .connent_sql = sqlite3 .connect ("wangyiyunyinyue.db" )
3848 self .cursor = self .connent_sql .cursor ()
3949 data = "INSERT INTO song_information (song_id,singer,song_name,alium,lyric) VALUES ('{}','{}','{}','{}','{}')" .format (song_id ,singer ,song_name ,alium ,lyric )
@@ -42,56 +52,90 @@ def sava_sql(self,song_id,singer,song_name,alium,lyric):
4252 self .connent_sql .commit ()
4353 self .connent_sql .close ()
4454
55+ def comment_sql (self ,song_id ,userid ,nickname ,comment ):
56+ self .sql ()
57+ self .connent_sql = sqlite3 .connect ("wangyiyunyinyue.db" )
58+ self .cursor = self .connent_sql .cursor ()
59+ data = "INSERT INTO comment_text (song_id,userId,nickname,comment) VALUES ('{}','{}','{}','{}')" .format (song_id ,userid ,nickname ,comment )
60+ self .cursor .execute (data )
61+ self .cursor .close ()
62+ self .connent_sql .commit ()
63+ self .connent_sql .close ()
64+
4565 def check_sql (self ):
4666 self .connent_sql = sqlite3 .connect ("wangyiyunyinyue.db" )
4767 self .cursor = self .connent_sql .cursor ()
68+ # date = "SELECT comment FROM song_information,comment_text WHERE song_information.song_id==comment_text.song_id"
4869 date = "SELECT * FROM song_information"
4970 a = self .cursor .execute (date )
71+ x = 1
5072 for i in a :
51- print (i )
73+ print (x ,base64 .b64decode (str .encode (i [0 ])).decode ("utf-8" ))
74+ x += 1
75+
76+ def song (self ,id ):
77+ song_url = "http://music.163.com/song?id={}" .format (id )
78+ ressong = requests .get (song_url ,headers = self .headers ,proxies = self .proxies ,timeout = 10 )
79+ Bs = BeautifulSoup (ressong .text ,'lxml' )
80+ song_name = Bs .find ("div" ,class_ = "cnt" ).find_all ("div" ,class_ = "tit" )
81+ singer_and_alium = Bs .find ("div" ,class_ = "cnt" ).find_all ('p' )
82+ song_information = []
83+ for sn in song_name :
84+ song_information .append (base64 .b64encode (str .encode (sn .get_text ().strip ())))
85+ for sa in singer_and_alium :
86+ song_information .append (base64 .b64encode (str .encode (sa .get_text ().split (":" )[1 ])))
87+ try :
88+ print ("歌曲:" ,base64 .b64decode (song_information [0 ]).decode ("utf-8" ),
89+ "歌手:" ,base64 .b64decode (song_information [1 ]).decode ("utf-8" ),
90+ "专辑:" ,base64 .b64decode (song_information [2 ]).decode ("utf-8" ))
91+ self .song_sql (id ,song_information [1 ].decode ("utf-8" ),song_information [0 ].decode ("utf-8" ),song_information [2 ].decode ("utf-8" ),"lyric/{}/{}.lrc" .format (self .url .split ("=" )[- 1 ],id ))
92+ except KeyError :
93+ pass
94+ except UnicodeEncodeError :
95+ print ("GBK" ) # windows下 GBK错误~
96+
97+ def lyric (self ,id ):
98+ lyric_url = "http://music.163.com/api/song/lyric?id={}&lv=-1&kv=-1&tv=-1" .format (id )
99+ reslyric = requests .get (lyric_url ,headers = self .headers ,proxies = self .proxies ,timeout = 10 )
100+ sp_url = self .url .split ("=" )[- 1 ]
101+ try :
102+ English_lyric = reslyric .json ()['lrc' ]['lyric' ]
103+ Chinese_lyric = reslyric .json ()['tlyric' ]['lyric' ]
104+ with open ("lyric/{}/{}.lrc" .format (sp_url ,id ),'a' ) as w :
105+ w .write (English_lyric )
106+ w .write (Chinese_lyric )
107+ except TypeError :
108+ pass # 有些歌词,要么只有英文,要么只有中文,要么连歌词都没有~~~:)
109+ except UnicodeEncodeError : # windows下 GBK错误~
110+ pass
111+
112+ def comment (self ,id ):
113+ comment_url = "http://music.163.com/api/v1/resource/comments/R_SO_4_{}" .format (id )
114+ rescomment = requests .get (comment_url ,headers = self .headers ,proxies = self .proxies ,timeout = 10 )
115+ for cm in range (len (rescomment .json ()['hotComments' ])):
116+ userid = rescomment .json ()['hotComments' ][cm ]['user' ]['userId' ]
117+ nickname = rescomment .json ()['hotComments' ][cm ]['user' ]['nickname' ]
118+ comment = rescomment .json ()['hotComments' ][cm ]['content' ]
119+ self .comment_sql (id ,userid ,base64 .b64encode (str .encode (nickname )).decode ("utf-8" ),base64 .b64encode (str .encode (comment )).decode ("utf-8" ))
52120
53121 def main (self ):
54122 playlist_url = "http://music.163.com/api/v3/playlist/detail?id={}" .format (self .url .split ("=" )[- 1 ])
55123 reslist = requests .get (playlist_url ,headers = self .headers ,proxies = self .proxies ,timeout = 10 )
124+ if os .path .exists ("lyric" ) == False :
125+ os .mkdir ("lyric" )
126+ if os .path .exists ("lyric/{}" .format (self .url .split ("=" )[- 1 ])) == False :
127+ os .mkdir ("lyric/{}" .format (self .url .split ("=" )[- 1 ]))
56128 try :
57129 for ids in reslist .json ()['playlist' ]['trackIds' ]:
58- song_url = "http://music.163.com/song?id={}" .format (ids ['id' ])
59- lyric_url = "http://music.163.com/api/song/lyric?id={}&lv=-1&kv=-1&tv=-1" .format (ids ['id' ])
60- ressong = requests .get (song_url ,headers = self .headers ,proxies = self .proxies ,timeout = 10 )
61- Bs = BeautifulSoup (ressong .text ,'lxml' )
62- song_name = Bs .find ("div" ,class_ = 'cnt' ).find_all ("div" ,class_ = 'tit' )
63- singer_and_alium = Bs .find ("div" ,class_ = 'cnt' ).find_all ('p' )
64- song_information = []
65- for sn in song_name : #这里因为存不进去~所以改一下编码~
66- song_information .append (base64 .b64encode (str .encode (sn .get_text ().strip ())))
67- for sa in singer_and_alium :
68- song_information .append (base64 .b64encode (str .encode (sa .get_text ().split (":" )[1 ])))
69130 time .sleep (np .random .random ())
70- reslyric = requests .get (lyric_url ,headers = self .headers ,proxies = self .proxies ,timeout = 10 )
71- try :
72- English_lyric = reslyric .json ()['lrc' ]['lyric' ]
73- Chinese_lyric = reslyric .json ()['tlyric' ]['lyric' ]
74- print ("歌名:" ,base64 .b64decode (song_information [0 ]).decode ("utf-8" ),
75- "歌手:" ,base64 .b64decode (song_information [1 ]).decode ("utf-8" ),
76- "专辑:" ,base64 .b64decode (song_information [2 ]).decode ("utf-8" ))
77- self .sava_sql (ids ['id' ],song_information [1 ].decode ("utf-8" ),song_information [0 ].decode ("utf-8" ),song_information [2 ].decode ("utf-8" ),"chesi/{}.lrc" .format (ids ['id' ]))
78- try :
79- with open ("lyric/{}.lrc" .format (ids ['id' ]),'a' ) as w :
80- w .write (English_lyric )
81- w .write (Chinese_lyric )
82- except TypeError : # 有些歌词,要么只有英文,要么只有中文,要么连歌词都没有~~~:)
83- pass
84- except KeyError :
85- pass
86- except UnicodeEncodeError :
87- pass # windows下 GBK错误~
131+ self .song (ids ['id' ])
132+ self .lyric (ids ['id' ])
133+ self .comment (ids ['id' ])
88134 except KeyError :
89135 print ("url地址不对~根本就没这个歌单嘛~~\t -_-!!" )
90136
91137if __name__ == "__main__" :
92- url = "http://music.163.com/#/playlist?id=864401021" #填入歌单的url地址~比如:http://music.163.com/#/playlist?id=864401021
93- if os .path .exists ("lyric" ) == False :
94- os .mkdir ("lyric" )
138+ url = "" # 填入歌单的url地址~比如:http://music.163.com/#/playlist?id=864401021
95139 if url == "" :
96140 print ("你url地址还没输呐~~" )
97141 else :
0 commit comments