如何爬取周杰伦的歌曲名 运用pandas、requests、BeautifulSoup库
时间: 2023-12-02 17:02:34 浏览: 114
首先,我们需要导入所需的库:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
```
然后,我们可以利用requests库获取周杰伦的歌曲页面:
```python
url = 'https://baike.baidu.com/item/周杰伦/227181'
response = requests.get(url)
```
接着,我们可以用BeautifulSoup库对获取的页面进行解析:
```python
soup = BeautifulSoup(response.content, 'html.parser')
```
我们可以通过查看页面源代码,发现周杰伦的歌曲名保存在一个class为"songlists"的div标签中,因此我们可以通过以下代码获取所有歌曲名:
```python
song_list = []
for tag in soup.find_all('div', {'class': 'songlists'}):
for li_tag in tag.find_all('li'):
song_list.append(li_tag.text.strip())
```
最后,我们可以将歌曲名保存到一个DataFrame中,并输出到csv文件:
```python
df = pd.DataFrame({'song_name': song_list})
df.to_csv('jay_chou_songs.csv', index=False)
```
完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://baike.baidu.com/item/周杰伦/227181'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
song_list = []
for tag in soup.find_all('div', {'class': 'songlists'}):
for li_tag in tag.find_all('li'):
song_list.append(li_tag.text.strip())
df = pd.DataFrame({'song_name': song_list})
df.to_csv('jay_chou_songs.csv', index=False)
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)