forked from ma6174/fmpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_sogou_mp3.py
44 lines (42 loc) · 1.41 KB
/
get_sogou_mp3.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
#!/usr/bin/env python
#coding=utf-8
import urllib
import urllib2
import re
def getlink(music_name):
#quote_name = urllib.quote(music_name.decode('utf-8').encode('gbk'))
quote_name = urllib.quote(music_name)
query_url='http://mp3.sogou.com/music.so?query=%s&class=1&pf=&w=02009900&st=&ac=1&sourcetype=sugg&_asf=mp3.sogou.com&_ast=1361525645'%quote_name
data = urllib2.urlopen(query_url,timeout=10).read()
re_com = re.compile('站点" onclick="window.open\(\'(.*?)\'')
forward_links = re_com.findall(data)
total = len(forward_links)
print total
if total == 0:
return None
for i in range(total):
next_link = "http://mp3.sogou.com"+forward_links[i]
#next_link = forward_links[i]
data2 = urllib2.urlopen(next_link,timeout=10).read()
re_com2 = re.compile('<div class="btn_area">*\s*<a href=\"(.*?)\"')
download_link = re_com2.findall(data2)
try:
if download_link[0][-3:] == 'mp3':
#print download_link[0]
return download_link[0]
except:
pass
return None
def get_all_mp3(url):
'''get all mp3 from a url'''
data = urllib2.urlopen(url).read()
re_com = re.compile('http://.*?\.mp3')
all = re_com.findall(data)
re_com = re.compile('<a href=\"(.*?\.mp3)\"')
ll = re_com.findall(data)
for i in ll:
if urllib.basejoin(url,i) not in all:
all.append(urllib.basejoin(url,i))
return list(set(all)) #删除重复歌曲
if __name__=='__main__':
print getlink('寂寞先生')