看到网上其他同学的思路是爬取所有歌单,然后筛选出评论过万的歌曲。但我觉得不同歌单之间会有交叉,这种方式可能效率不高,而且可能会有漏网之鱼。所以我准备爬取所有歌手,再爬取他们的热门50单曲,从中筛选评论过万的歌曲。现阶段几乎没有歌手有超过50首评论过万的歌曲,所以这种方法目前是可行的。
查看歌手页面,歌手被分成了华语男歌手、华语女歌手、欧美男歌手……共计15个板块,板块代号如下:
group = ['1001', '1002', '1003', '2001', '2002', '2003', '6001', '6002', '6003', '7001', '7002', '7003', '4001', '4002', '4003']
而每个板块又按照首字母分成了27个子页面(包括热门歌手页面),子页面代号如下:
initial = ['0']
for i in range(65, 91):
initial.append(str(i))
15*27=405,我们要爬取405个歌手子页面,可以利用上述代号拼接出这405个歌手子页面链接:
urls = []
for g in group:
for i in initial:
url = 'http://music.163.com/discover/artist/cat?id=' + g + '&initial=' + i
urls.append(url)
然后就是用爬虫从这些页面上爬取歌手的id:
def get_artist(url):
k = 0
t = []
while True:
try:
resp = request.urlopen(url)
html = resp.read().decode('utf-8')
soup = bs(html, 'html.parser')
l = soup.find_all('a', class_='nm nm-icn f-thide s-fc0')
p = r'\s*\/[a-z]+\?[a-z]+\=([0-9]+)'
for i in l:
t.append(re.match(p, i['href']).group(1))
return t
except Exception as e:
print(e)
k += 1
if k > 10:
print('页面' + url + '发生错误')
return None
t = []
continue
获得歌手id以后,再让爬虫爬取歌手的个人页面,获取热门50单曲的歌曲id:
def get_song(artist_id):
k = 0
t = []
while True:
url = 'http://music.163.com/artist?id=' + artist_id
try:
req = request.Request(url)
req.add_header('User-Agent',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4399.400 QQBrowser/9.7.12777.400')
req.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
resp = request.urlopen(req)
html = resp.read().decode('utf-8')
soup = bs(html, 'html.parser')
except Exception as e:
k += 1
if k > 10:
print('歌手' + artist_id + '发生错误')