一、需求
Python爬取网易云音乐所有歌手的名称和链接
二、代码实现
import requests
from lxml
import etree
from urllib
import request
import time
class WangYiYun:
def __init__(self
, base_url
):
self
.html
= self
.request_url
(base_url
)
self
.parse_html
()
def request_url(self
, base_url
):
headers
= {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
self
.html
= requests
.get
(url
=base_url
, headers
=headers
).content
.decode
('utf-8')
self
.tree
= etree
.HTML
(self
.html
)
return self
.tree
def parse_html(self
):
group_list
= self
.html
.xpath
('//div[@class = "blk"]')
for group
in group_list
:
href_list
= group
.xpath
('.//a/@href')
group_name_list
= group
.xpath
('.//a/text()')
for href
, group_name
in zip(href_list
, group_name_list
):
full_href
= request
.urljoin
('https://music.163.com', href
)
html1
= requests
.get
(full_href
).content
.decode
('utf-8')
tree1
= etree
.HTML
(html1
)
url_list
= tree1
.xpath
('//ul[@class="n-ltlst f-cb"]/li[position()>1]/a/@href')
for url
in url_list
:
full_url
= request
.urljoin
('https://music.163.com', url
)
single_letter
= requests
.get
(full_url
).content
.decode
('utf-8')
single_letter_tree
= etree
.HTML
(single_letter
)
li_list
= single_letter_tree
.xpath
('//ul[@class="m-cvrlst m-cvrlst-5 f-cb"]/li')
for li
in li_list
:
name_list
= li
.xpath
('//a[@class="nm nm-icn f-thide s-fc0"]/text()')
link_list
= li
.xpath
('//a[@class="nm nm-icn f-thide s-fc0"]/@href')
for name
, link
in zip(name_list
, link_list
):
full_link
= 'https://music.163.com' + link
print(name
, full_link
)
time
.sleep
(0.5)
if __name__
== '__main__':
base_url
= 'https://music.163.com/discover/artist'
WangYiYun
(base_url
)
三、程序运行结果
转载请注明原文地址:https://blackberry.8miu.com/read-32742.html