豆瓣电影《穿靴子的猫2》影评爬取与分析
import requests\nfrom bs4 import BeautifulSoup\n\n# 设置请求头信息\nheaders = {\n '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'}\n\n# 穿靴子的猫2在豆瓣电影上的链接\nurl = 'https://movie.douban.com/subject/25868125/'\n\n# 发送请求\nresponse = requests.get(url, headers=headers)\n\n# 使用BeautifulSoup解析网页\nsoup = BeautifulSoup(response.text, 'html.parser')\n\n# 获取总评价数\ntotal_comment = soup.find('span', {'class': 'rating_num'}).string\nprint('总评价数:' + total_comment)\n\n# 计算总页数\ntotal_page = int(total_comment) // 20 + 1\nprint('总页数:' + str(total_page))\n\n# 循环爬取每一页的影评数据\nfor i in range(total_page):\n # 构造URL\n page_url = url + 'comments?start=' + str(i * 20) + '&limit=20&sort=new_score&status=P'\n\n # 发送请求\n response = requests.get(page_url, headers=headers)\n\n # 使用BeautifulSoup解析网页\n soup = BeautifulSoup(response.text, 'html.parser')\n\n # 获取影评\n comments = soup.find_all('span', {'class': 'short'})\n\n # 将影评写入文件\n with open('comments.txt', 'a', encoding='utf-8') as f:\n for comment in comments:\n f.write(comment.get_text() + '\n')\n\n# 将影评读取出来并输出前5条\nwith open('comments.txt', 'r', encoding='utf-8') as f:\n comments = f.readlines()\n for i in range(5):\n print('影评' + str(i+1) + ':' + comments[i])
原文地址: https://www.cveoy.top/t/topic/oBg4 著作权归作者所有。请勿转载和采集!