🎓 腾讯课堂评论爬虫

📋 功能说明
  • 爬取课程评论区内容
  • 分析课程好评度
  • 批量采集用户评价
💻 源代码
import requests
import json

def get_tencent_comments(course_id, page=1):
    """获取腾讯课堂评论"""
    url = f'https://ke.qq.com/cgi-bin/comment/get_comment_list'
    
    params = {
        'cid': course_id,
        'page': page,
        'pagesize': 20
    }
    
    headers = {
        'User-Agent': 'Mozilla/5.0 Chrome/91.0.4472.124',
        'Referer': f'https://ke.qq.com/course/{course_id}'
    }
    
    res = requests.get(url, params=params, headers=headers).json()
    comments = []
    
    for item in res.get('result', {}).get('comment_list', []):
        comments.append({
            'user': item['user_info']['nick'],
            'content': item['content'],
            'score': item['score'],
            'time': item['create_time']
        })
    
    return comments

def analyze_course(course_id):
    """分析课程评价"""
    all_comments = []
    for page in range(1, 6):  # 最多5页
        comments = get_tencent_comments(course_id, page)
        if not comments:
            break
        all_comments.extend(comments)
    
    # 计算平均分
    total_score = sum(c['score'] for c in all_comments)
    avg_score = total_score / len(all_comments) if all_comments else 0
    
    return {
        'total_comments': len(all_comments),
        'avg_score': round(avg_score, 2),
        'comments': all_comments[:10]
    }

# 示例
analysis = analyze_course(123456)
print(f"总评论: {analysis['total_comments']}, 平均分: {analysis['avg_score']}")
📦 运行环境
pip install requests
使用说明
  1. 打开腾讯课堂课程页
  2. 从URL获取course_id
  3. 调用API获取评论