🎬 猫眼电影影评爬虫

📋 功能说明
  • 爬取电影影评内容
  • 提取用户评分和评论
  • 批量采集评论数据
💻 源代码
import requests
import json

def get_maoyan_comments(movie_id, offset=0, limit=20):
    """获取猫眼电影评论"""
    url = 'https://piaofang.maoyan.com/movie/' + str(movie_id) + '/comments'
    
    params = {
        'page': offset // limit + 1,
        'limit': limit,
        'type': 1
    }
    
    headers = {
        'User-Agent': 'Mozilla/5.0 Chrome/91.0.4472.124',
        'Referer': 'https://piaofang.maoyan.com/'
    }
    
    res = requests.get(url, params=params, headers=headers).json()
    
    comments = []
    for item in res.get('cmts', []):
        comment = {
            'user': item.get('nickName'),
            'score': item.get('score'),
            'content': item.get('content'),
            'time': item.get('time'),
            'city': item.get('cityName'),
            'like': item.get('approve')
        }
        comments.append(comment)
    
    return comments

def get_hot_movies():
    """获取热门电影"""
    url = 'https://piaofang.maoyan.com/movie/movie-list'
    headers = {'User-Agent': 'Mozilla/5.0'}
    res = requests.get(url, headers=headers).json()
    
    movies = []
    for movie in res.get('movieList', [])[:10]:
        movies.append({
            'id': movie.get('movieId'),
            'name': movie.get('movieName'),
            'box_office': movie.get('boxInfo')
        })
    return movies

# 示例
movies = get_hot_movies()
for m in movies[:5]:
    print(f"电影: {m['name']}, 票房: {m['box_office']}")
📦 运行环境
pip install requests
功能特点
  • ✅ 影评采集
  • ✅ 评分提取
  • ✅ 批量采集