Python实现网易云热门歌单微博发布

前言

网易云音乐是当前比较流行的音乐网站,每天都会推荐热门歌单,正好自己也喜欢听音乐,就想能不能用python实现一个云音乐歌单的自动获取和通过微博发布的功能。

正文

简单思路是这样的,用Python每隔一段时间去获取页面信息,然后跟之前获取的信息作比较,如果一样就不管,反之说明有更新,这时候把更新的歌单名,歌单封面,歌单链接通过微博发布出来。

Python的sinaweibopy提供了一种很方便的途径发送微博,只需要提前去微博开放平台注册一下,然后申请 应用开发 -> 移动应用 然后根据个人信息填写,之后会收到邮件 里边有app key, app secret 。

注意!! 非常重要!去应用信息-高级信息里面填写回调页 这是什么我也不知道!但是照着填!我填的是 https://api.weibo.com/oauth2/default.html 这个需要和之后的python code里面的 callback url一致!!!

代码

需要用到的包

  • time
  • sinaweibopy
  • requests
  • beautifulsoup4
  • urllib

main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import time
from weibo import APIClient
import requests
import bs4
from urllib import urlretrieve
def getmusic():
url = 'http://music.163.com/discover'
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\
/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36'}
s = requests.session()
try:
print 'Get music now'
htmllist = s.get(url,headers=header)
bsObjlist = bs4.BeautifulSoup(htmllist.text,'html.parser')
ls = bsObjlist.find_all('div',{'class':'u-cover u-cover-1'})
count = 0
templist = []
while(count < 8):
picurl = ls[count].find('img').attrs['src']
picurl = picurl.split('param')[0] + 'param=400y400'
title = ls[count].find('a',{'class':'msk'}).attrs['title']
aurl = 'http://music.163.com' + ls[count].find('a',{'class':'msk'}).attrs['href']
temp = [picurl,title,aurl]
templist.append(temp)
count += 1
return templist
except Exception as e:
print e
pass
def getTitleList(templist):
tlist = []
for item in templist:
tlist.append(item[1])
return tlist
def get_access_token(app_key, app_secret, callback_url):
client = APIClient(app_key=app_key, app_secret=app_secret, redirect_uri=callback_url)
auth_url = client.get_authorize_url()
print auth_url
code = raw_input("Input code:")
r = client.request_access_token(code)
access_token = r.access_token
expires_in = r.expires_in
print 'access_token:',access_token
print 'expires_in:', expires_in
return access_token, expires_in
if __name__ == '__main__':
app_key = '3828611682'
app_secret = '905036e24f75c47147da46b0df9e04c0'
callback_url = 'https://api.weibo.com/oauth2/default.html'
access_token, expires_in = get_access_token(app_key, app_secret, callback_url)
client = APIClient(app_key=app_key, app_secret=app_secret, redirect_uri=callback_url)
client.set_access_token(access_token, expires_in)
musiclist = []
print 'start'
while True:
try:
templist = getmusic()
for item in templist:
if not item[1] in musiclist:
print 'try to send ' + item[1]
try:
urlretrieve(item[0],'temp.jpg')
str = item[1] + item[2]
f = open('temp.jpg', 'rb')
r = client.statuses.upload.post(status=str, pic=f)
f.close()
print 'send ' + item[1] + ' successfully!'
print 'sleep for 30s'
print ' '
except Exception as e:
print e
pass
time.sleep(30)
else:
print item[1] + ' repeated!'
musiclist = getTitleList(templist)
print 'sleep for 200s'
print ' '
time.sleep(200)
except Exception as e:
print e
pass

成果

后记

  1. 页面的爬取没有什么难度,有的就是网易云的歌单播放量有的有‘万’字,有的没有,要从有‘万’字的歌单里面挑一个最高的来发布。

  2. sinaweibopy目前好像只支持python2,我一直用的3,所以写的过程很蛋疼,电脑换来换去。

  3. 虽然我设置了20s检测一次,不过网易云的歌单更新还是比较慢的….好几天播放量最高的才换一个,一开始我想推荐歌单里面的歌曲,不过字数超过140了,以后再想想发布的内容更丰富一点。

附源码地址:https://github.com/xucheng7112/weibo