python 获取百度音乐歌手歌曲的播放链接
from selenium
import webdriver
from time
import sleep
class MusicLink(object):
def __init__(self
, name
, url
="http://music.taihe.com"):
"""初始化浏览器驱动、获取歌手信息"""
self
.drive
= webdriver
.Chrome
(r
'C:\Program Files\Python3\chromedriver.exe')
self
.drive
.implicitly_wait
(10)
self
.url
= url
print("当前音乐站点是:%s" % self
.url
)
self
.name
= name
self
.drive
.get
(self
.url
)
sleep
(3)
def search_singer(self
):
"""获取歌手歌曲页"""
self
.drive
.find_element_by_class_name
("el-input__inner").send_keys
(self
.name
)
self
.drive
.find_element_by_class_name
('iconfont').click
()
sleep
(3)
self
.drive
.find_element_by_css_selector
('#tab-2').click
()
singers
= self
.drive
.find_elements_by_css_selector
('div.el-col:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1)')
for singer
in singers
:
if singer
.text
== self
.name
:
singer
.click
()
sleep
(3)
pages
= self
.drive
.find_elements_by_css_selector
('.song-box .el-pagination ul > li')
if len(pages
) > 0:
for page
in pages
:
page
.click
()
sleep
(3)
print('----------第 %d 页------------' % int(page
.text
))
self
.song_link
()
sleep
(2)
else:
self
.song_link
()
self
.drive
.quit
()
def song_link(self
):
"""获取歌曲播放链接"""
links
= self
.drive
.find_elements_by_css_selector
('.song-box > div > ul > li div > .song-box > a')
for link
in links
:
print(link
.text
, link
.get_attribute
('href'))
sleep
(0.1)
if __name__
== '__main__':
md
= MusicLink
(input("请输入歌手名:"))
md
.search_singer
()
新手小白,如有错误之处,请多多指教,在此感谢。