用python写一个爬取QQ音乐歌曲的爬虫
时间: 2024-05-03 18:22:44 浏览: 102
以下是一个简单的爬取QQ音乐的歌曲信息的Python爬虫:
```python
import requests
from bs4 import BeautifulSoup
def get_songs(keyword):
url = 'https://c.y.qq.com/soso/fcgi-bin/search_cp?'
params = {
'ct': '24',
'qqmusic_ver': '1298',
'new_json': '1',
'remoteplace': 'sizer.yqq.song_next',
'searchid': '64405487069162918',
't': '0',
'aggr': '1',
'cr': '1',
'catZhida': '1',
'lossless': '0',
'flag_qc': '0',
'p': '1',
'n': '20',
'w': keyword,
'g_tk_new_20200303': '5381',
'g_tk': '5381',
'loginUin': '0',
'hostUin': '0',
'format': 'json',
'inCharset': 'utf8',
'outCharset': 'utf-8',
'notice': '0',
'platform': 'yqq.json',
'needNewCode': '0'
}
headers = {
'referer': 'https://y.qq.com/portal/search.html',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
data = response.json()
song_list = data['data']['song']['list']
for song in song_list:
song_name = song['name']
singer_name = song['singer'][0]['name']
album_name = song['album']['name']
print(f'Song Name: {song_name}, Singer: {singer_name}, Album: {album_name}')
else:
print('Failed to get songs.')
if __name__ == '__main__':
keyword = input('Please enter the keyword: ')
get_songs(keyword)
```
该爬虫使用了requests库和BeautifulSoup库,实现了自动搜索并爬取QQ音乐上关于指定歌曲的歌曲名、歌手名和专辑名,并输出到控制台。其中,url和params参数是通过分析QQ音乐搜索接口得到的,headers参数是通过在浏览器中查看请求头信息得到的。
阅读全文