python+request网易云歌单爬取

 爬取该网页的歌单:url(https://music.163.com/#/playlist?id=2395304063

python+request网易云歌单爬取

#coding:utf-8
import requests
import re
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
    'Referer': 'https://music.163.com/',
    'Connection': 'keep-alive',
    'Origin': 'https://music.163.com',
    'Host': 'music.163.com',
}
url  ='https://music.163.com/playlist?id=2395304063'
data = {"params": "jCeAzown0/N4HpaMxZYwbGL4BmKFdJOZlJYvhMwXa4Wh2JR6FZ+IwZV24RJMwynkKmlsmNVwZPQRPILAiCU4aQhWEHETSKMDYmx56P/b2UO62AARmQhsBIteI59Ne+OsMs9MwPZJ9hrxi30+JpetIqqW9svBnV9ZJJ1cE5OZ0L1PEViLfGxTG17a6GZKWwJ5HO3VG5MzbeYsY3yo8r3sUbvfhO5lpMzrrWWYolQDjx4=",
        "encSecKey": "16f20abc878350f8a1c7def04a64230d6ad621f71c166fb15a3b78e7452d9180173df5c87367fb2a08aeea33d517420f3caada6af565dd075213d9a286da5ae849d34899ad0bcfe9601c1fe6e1fabf129b171073416c930bc180cc5aa5e3dbe0377e3dadc1ba2f35eef07778043a2ab30dc26f76f8f29d73704f0c4776445187"
        }
# res = requests.post(url=url, data=data, headers=headers)
res = requests.get(url=url, headers=headers)
html = res.text
song = re.compile(r'<li><a href="(.+?)">.+?</a></li>')
song_res = re.findall(song, html)
song_res = ' '.join(song_res)
song_res = song_res.split(' ')

# print(song_res)
for i in song_res:

    if i != '#':
        # print(i)
        msg_url = 'https://music.163.com'+i
        print(msg_url)
        msg_res = requests.get(url=msg_url, headers=headers)
        html = msg_res.text
        song_title = r'"title": "([\s\S]*?)",'
        song_singer = r'"description": "([\s\S]*?)",'
        song_date = r'"pubDate": "(.+?)"'
        song_title = re.findall(song_title, html)
        song_singer = re.findall(song_singer, html)
        song_date = re.findall(song_date, html)
        print('歌曲名为:', song_title[0])
        print('歌手:', song_singer[0])
        print('时间:', song_date[0])

运行结果为:

python+request网易云歌单爬取