import requests
from bs4 import BeautifulSoup
from lxml import etree #用来预处理
#请求数据
url = 'https://music.163.com/discover/toplist?id=19723756'
#伪装自己
headers = {
'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',
'referer': 'https://music.163.com/'
}
response = requests.get(url=url,headers=headers,params=False).content.decode("utf-8")
s = BeautifulSoup(response, 'lxml')
main = s.find('ul', {'class': 'f-hide'})
list= main.find_all("a")
for i in list:
name = i.text
music_url = 'http://music.163.com/song/media/outer/url' + i['href'][5:] + '.mp3'
music = requests.get(music_url).content
with open('D:/NBA/'+name + '.mp3', mode='wb') as file:
file.write(music)
为啥爬取下来的音乐大小都是1kb,打开说格式错误或者文件已经损坏
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 我不喜欢这个世界 2021-03-21 22:04关注
import requests from bs4 import BeautifulSoup from lxml import etree #用来预处理 #请求数据 url = 'https://music.163.com/discover/toplist?id=19723756' #伪装自己 headers = { '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', 'referer': 'https://music.163.com/' } response = requests.get(url=url,headers=headers,params=False).content.decode("utf-8") s = BeautifulSoup(response, 'lxml') main = s.find('ul', {'class': 'f-hide'}) list= main.find_all("a") for i in list: name = i.text music_url = 'http://music.163.com/song/media/outer/url' + i['href'][5:] + '.mp3' response = requests.get(music_url,headers=headers,allow_redirects=False) url = response.headers.get('Location') music = requests.get(url,headers=headers).content with open('D:/NBA/' + name + '.mp3', mode='wb') as file: file.write(music)
请求中有重定向,禁止以后,再次访问重定向的链接就行了
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 【急】在线问答CNC雕刻机的电子电路与编程
- ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
- ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
- ¥15 Python pandas
- ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
- ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
- ¥15 云环境云开发云函数对接微信商户中的分账功能
- ¥15 空间转录组CRAD遇到问题
- ¥20 materialstudio计算氢键脚本问题
- ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)