当前位置:   article > 正文

利用requests爬取网易云音乐_使用beautifulsoup爬取网易云

使用beautifulsoup爬取网易云

1、打开网易云选择喜欢的歌手

2、爬取网站

3、创建文件夹

  1. import requests
  2. from bs4 import BeautifulSoup
  3. def music():
  4. url = 'https://music.163.com/artist?id=29051613'
  5. #伪装自己
  6. headers = {
  7. 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.1071 SLBChan/21',
  8. 'referer': 'https://music.163.com/'
  9. }
  10. data = {
  11. "params": "+H6D23233AX7 + ZhHr4HqHLeDwi5A9nhbtTqkxhoFC / A9Jf01CuoQpZ + AX7YFx + +W",
  12. "encSecKey": "0b87f8306db58707587ee170832712c5781f96adc1890048e339977a819f26b86f814c91b2bafbb123a35de4da4d16678f1f5ca2af99657b48048a30dad0b18ed14e2f32c23145c6e42349af1356287119e7b1157b544f292b212d7014168b22605b83692af7fc5c4fd3395c86fa7ea9395b4b9fb560b83268044be825c61723"
  13. }
  14. response = requests.get(url=url,headers=headers,params=False,data=data).content.decode("utf-8")
  15. s = BeautifulSoup(response, 'lxml')
  16. main = s.find('ul', {'class': 'f-hide'})
  17. list= main.find_all("a")
  18. for i in list:
  19. name = i.text.replace("/"," ")
  20. music_url = 'http://music.163.com/song/media/outer/url' + i['href'][5:] + '.mp3'
  21. # 第一次没有这两行代码,所以下载出来文件是错误的。因为请求中有重定向,禁止以后,再次访问重定向的链接就行了
  22. response = requests.get(music_url, headers=headers, allow_redirects=False, data=data)
  23. url = response.headers.get('Location')
  24. music = requests.get(music_url,headers=headers,data=data).content
  25. with open('D:/郑润泽/'+name + '.mp3', mode='wb') as file:
  26. file.write(music)
  27. print(name + "正在下载")
  28. print("over!!!")
  29. if __name__ == '__main__':
  30. music()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/409147
推荐阅读
相关标签
  

闽ICP备14008679号