访问网易云音乐,查找搜索接口的信息,发现搜索的接口没有想要的信息。
我又去找其他接口,最后发现信息在这里
而且请求的接口是post的请求头
我换了种方式爬取,用selnium的方式
最后的成果是这样的`
不打了,手累,不懂得可以问我。
import requests
from selenium import webdriver
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36',
}
a=input('输入名字:')
options = webdriver.ChromeOptions()
chrome_prefs = {}
options.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}
options.add_argument('--headless')
diver=webdriver.Chrome(options=options)
s=diver.get('https://music.163.com/#/search/m/?s={}&type=1'.format(a))
diver.switch_to.frame("g_iframe")
c=diver.find_elements_by_xpath('//div[@class="n-srchrst"]/div[@class="srchsongst"]/div')
p=0
d=[]
nameid=[]
for i in c:
p=p+1
id=i.find_element_by_xpath('./div[1]/div/a').get_attribute('data-res-id')
name=i.find_element_by_xpath('./div[1]/div/a').get_attribute('data-res-data')
musicname=i.find_element_by_xpath('./div[2]/div/div/a/b').get_attribute('title')
song = i.find_element_by_xpath('./div[4]/div/a').text
d.append(id)
nameid.append(musicname)
print("{}.".format(p),musicname,song)
inputname=int(input("请输入选择的序号:"))
try:
r=requests.get('http://music.163.com/song/media/outer/url?id={}.mp3'.format(d[inputname-1]),headers=headers)
with open('{}.mp3'.format(nameid[inputname-1]),'wb')as f:
f.write(r.content)
print("下载成功")
except:
print("下载失败")
转载需要表明出处