用Python写一段QQ音乐歌手信息爬虫代码
时间: 2023-06-08 20:03:56 浏览: 128
好的,以下是用Python写的QQ音乐歌手信息爬虫代码:
```python
import requests
from bs4 import BeautifulSoup
# 要爬取的歌手的名字
singer_name = "周杰伦"
# 构建请求头部
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"}
# 构建请求URL
url = "https://c.y.qq.com/soso/fcgi-bin/client_search_cp?&cr=1&p=1&n=20&w=" + singer_name
# 发送HTTP请求
response = requests.get(url, headers=headers)
# 解析响应数据
soup = BeautifulSoup(response.text, 'html.parser')
# 获取歌手信息列表
singer_list = soup.find_all('li', class_='singer_list')
# 打印歌手信息
for singer in singer_list:
singer_name = singer.find('h3').get_text()
singer_info = singer.find('p').get_text()
print(singer_name + " - " + singer_info)
```
接下来,请问您还有其他问题吗?
阅读全文