使用python网络爬虫爬取QQ音乐的所有歌手名字
时间: 2023-12-25 11:05:41 浏览: 364
可以使用Python的requests库和BeautifulSoup库来实现。
首先,我们需要确定QQ音乐歌手列表的URL。通过观察QQ音乐的歌手列表页面,可以发现URL为:https://y.qq.com/portal/singer_list.html。
然后,可以使用requests库来发送GET请求,获取歌手列表页面的HTML源代码:
```python
import requests
url = 'https://y.qq.com/portal/singer_list.html'
response = requests.get(url)
html = response.text
```
接着,可以使用BeautifulSoup库来解析HTML源代码,提取歌手名字:
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
singer_list = soup.find_all('ul', class_='singer_list__list')[0]
singers = singer_list.find_all('a', class_='singer_list__item')
for singer in singers:
print(singer.text)
```
完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://y.qq.com/portal/singer_list.html'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
singer_list = soup.find_all('ul', class_='singer_list__list')[0]
singers = singer_list.find_all('a', class_='singer_list__item')
for singer in singers:
print(singer.text)
```
执行以上代码,即可输出QQ音乐的所有歌手名字。
阅读全文