脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 详解python selenium 爬取网易云音乐歌单名

详解python selenium 爬取网易云音乐歌单名

2021-06-09 00:42Mandy。 Python

这篇文章主要介绍了python selenium爬取网易云音乐歌单名,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

目标网站:

详解python selenium 爬取网易云音乐歌单名

首先获取第一页的数据,这里关键要切换到iframe里

详解python selenium 爬取网易云音乐歌单名

打印一下

详解python selenium 爬取网易云音乐歌单名

获取剩下的页数,这里在点击下一页之前需要设置一个延迟,不然会报错。

详解python selenium 爬取网易云音乐歌单名

结果:

详解python selenium 爬取网易云音乐歌单名

一共37页,爬取完毕后关闭浏览器

完整代码:

?
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
url = 'https://music.163.com/#/discover/playlist/'
 
from selenium import webdriver
import time
# 创建浏览器对象
window = webdriver.chrome('./chromedriver')
window.get(url)
time.sleep(1)
# 切换到iframe窗口
iframe = window.find_element_by_id('g_iframe')
window.switch_to.frame(iframe)
music_list = window.find_elements_by_xpath('//ul[@class="m-cvrlst f-cb"]/li')
for music_element in music_list:
 print(music_element.text)
 print()
print('*'*20)
 
page = 1
# 开始提取
while true:
 time.sleep(2)
 # 获取下一页
 next = window.find_element_by_link_text('下一页')
 if not next.get_attribute('class') == 'zbtn znxt js-disabled':
  next.click()
 else:
  break
 time.sleep(1)
 # 提取数据
 music_list = window.find_elements_by_xpath('//ul[@class="m-cvrlst f-cb"]/li')
 for music_element in music_list:
  print(music_element.text)
  print()
 print(page,'*'*20)
 page+=1
# 退出浏览器
window.quit()

以上所述是小编给大家介绍的python selenium爬取网易云音乐歌单名详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/weixin_43751840/article/details/88406610

延伸 · 阅读

精彩推荐