💻 源代码
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']}")