写一段用python爬虫网易云音乐歌手粉丝数的代码
时间: 2023-07-14 14:11:51 浏览: 257
下面是一个简单的用Python爬虫网易云音乐歌手粉丝数的代码,需要安装requests和beautifulsoup4两个库:
```python
import requests
from bs4 import BeautifulSoup
# 歌手ID
artist_id = '6452'
# 构造请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 发送HTTP请求
url = 'https://music.163.com/artist?id=' + artist_id
response = requests.get(url, headers=headers)
# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
fans = soup.find('strong', {'id': 'fan_count'}).text
print('该歌手粉丝数为:' + fans)
```
将`artist_id`变量替换为你想要查询的歌手的ID即可。
阅读全文