from bs4 import BeautifulSoup soup = BeautifulSoup(open('电影评论.html',encoding='utf-8'),'lxml') # 获取节点内容 tags_with_class = soup.find_all(class_='review-content clearfix') text_list = [] # 遍历结果集中的每个标签对象,并获取其文本内容 for tag in tags_with_class: text_list.append(tag.text) # 将列表转换为字符串 result_text = '\n'.join(text_list) # 打印文本内容 print(result_text) with open('评论.txt', 'w', encoding='utf-8') as fp: fp.write(result_text)