代码报错
import pprintimport os
import requests
import parsel
import re
filename = 'music\\'
if not os.path.exists(filename):
os.mkdir(filename)
url = 'https://www.kugou.com/yy/html/rank.html'
headers = {'user-agen':'Mzilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.55'}
response = requests.get(url=url,headers=headers)
# print(response.text)
selector = parsel.Selector(response.text)
href = selector.css('.pc_rank_sidebar li a::attr(href)').getall()
for index in href:
response_2 = requests.get(url=index, headers=headers)
Hash_list = re.findall('"Hash":"(.*?)"', response_2.text)
id_list = re.findall('"album_id":(\d+),',response_2.text)
zip_data = zip (Hash_list,id_list)
for link in zip_data:
hash = link
music_id = link
link_url = 'https://wwwapi.kugou.com/yy/index.php '
params = {
'r': 'play/getdata',
'callback':'jQuery1910140801497177923_1627403425983',
'hash': hash,
'dfid':'2Avi6d04PHEY48DIMp0wRG4k',
'mid':'fe9e2dfe307a2f0cfde7e6a474d2f98c',
'platid': '4',
'album_id': music_id,
'_': '1627403425984',
}
response_3 = requests.get(url=link_url,params=params,headers=headers)
pprint.pprint(response_3.json())
music_url = response_3.json()['data']['play_url']
music_name = response_3.json()['data']['audio_name']
music_content = requests.get(url=music_url,headers=headers).content
with open('music\\'+'.mp3',mode='wb') as f:
f.write(music_content)
print('正在保存:',music_name)
href = selector.css('.pc_rank_sidebar li a::attr(href)').getall()
这行代码有问题
话说,爬虫用requests和xpath会方便快捷太多
说实话css兼容性很差 href = selector.css('.pc_rank_sidebar li a::attr(href)').getall()
href = selector.css('.pc_rank_sidebar li a::attr(href)').getall() # ::改成英文的。
笨鸟学飞 发表于 2021-7-28 10:09
href = selector.css('.pc_rank_sidebar li a::attr(href)').getall()
这行代码有问题
话说,爬虫用req ...
谢谢了
页:
[1]