pandas 2.0.2版本 from bs4 import BeautifulSoup import pandas as pd import requests import time df = pd.read_csv('playlist.csv', header=None,error_bad_lines=False, names=['url', 'title', 'play', 'user']) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' } for i in df['url']: time.sleep(2) url = 'https://music.163.com' + i response = requests.get(url=url, headers=headers) html = response.text soup = BeautifulSoup(html, 'html.parser') # 获取歌单标题 title = soup.select('h2')[0].get_text().replace(',', ',') # 获取标签 tags = [] tags_message = soup.select('.u-tag i') for p in tags_message: tags.append(p.get_text()) # 对标签进行格式化 if len(tags) > 1: tag = '-'.join(tags) else: tag = tags[0] # 获取歌单介绍 if soup.select('#album-desc-more'): text = soup.select('#album-desc-more')[0].get_text().replace('\n', '').replace(',', ',') else: text = '无' # 获取歌单收藏量 collection = soup.select('#content-operation i')[1].get_text().replace('(', '').replace(')', '') # 歌单播放量 play = soup.select('.s-fc6')[0].get_text() # 歌单内歌曲数 songs = soup.select('#playlist-track-count')[0].get_text() # 歌单评论数 comments = soup.select('#cnt_comment_count')[0].get_text() # 输出歌单详情页信息 print(title, tag, text, collection, play, songs, comments) # 将详情页信息写入CSV文件中 with open('music_message.csv', 'a+', encoding='utf-8-sig') as f: f.write(title + ',' + tag + ',' + text + ',' + collection + ',' + play + ',' + songs + ',' + comments + '\n') # 获取歌单内歌曲名称 li = soup.select('.f-hide li a') for j in li: with open('music_name.csv', 'a+', encoding='utf-8-sig') as f: f.write(j.get_text() + '\n') 出错 read_csv() got an unexpected keyword argument 'error_bad_lines'
时间: 2023-09-24 11:11:51 浏览: 149
出错的原因是你使用的pandas版本(2.0.2)不支持在`read_csv()`函数中使用`error_bad_lines`参数。该参数是在较新版本的pandas中引入的。
解决方法是更新pandas到较新版本,或者删除`error_bad_lines`参数。如果你想删除该参数,则可以这样修改代码:
```python
df = pd.read_csv('playlist.csv', header=None, names=['url', 'title', 'play', 'user'], error_bad_lines=False)
```
这样修改后,`error_bad_lines`参数将被忽略,可以正常读取CSV文件。
阅读全文