1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| import os import requests from bs4 import BeautifulSoup import json
def get_douban_hot_movies(): """ 从豆瓣获取当前热映电影信息 """ url = "https://movie.douban.com/cinema/nowplaying/" headers = { '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' } try: response = requests.get(url, headers=headers) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') # 豆瓣热映电影信息通常在一个id为"nowplaying"的div中 nowplaying_div = soup.find('div', id='nowplaying') if not nowplaying_div: return [] movies = [] for li in nowplaying_div.find_all('li', class_='list-item'): movie = {} movie['id'] = li.get('id', '').strip() movie['title'] = li.get('data-title', '').strip() movie['score'] = li.get('data-score', '').strip() movie['duration'] = li.get('data-duration', '').strip() movie['region'] = li.get('data-region', '').strip() movie['director'] = li.get('data-director', '').strip() movie['actors'] = li.get('data-actors', '').strip() movie['poster'] = li.find('img').get('src') if li.find('img') else '' # 获取星级评分(如果有) star_rating = li.find('span', class_='subject-rate') if star_rating: movie['star_rating'] = star_rating.get_text().strip() else: movie['star_rating'] = '暂无评分' movies.append(movie) return movies except Exception as e: print(f"获取豆瓣热映电影失败: {e}") return []
def generate_html(movies): """ 生成展示热映电影的HTML页面 """ html_template = """ <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>豆瓣热映电影</title> <style> body {{ font-family: 'Helvetica Neue', Arial, sans-serif; line-height: 1.6; color: #333; max-width: 1200px; margin: 0 auto; padding: 20px; background-color: #f5f5f5; }} h1 {{ text-align: center; color: #007722; margin-bottom: 30px; }} .movie-container {{ display: flex; flex-wrap: wrap; justify-content: space-around; gap: 20px; }} .movie-card {{ width: 220px; background: white; border-radius: 5px; overflow: hidden; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); transition: transform 0.3s; }} .movie-card:hover {{ transform: translateY(-5px); }} .movie-poster {{ width: 100%; height: 300px; object-fit: cover; }} .movie-info {{ padding: 15px; }} .movie-title {{ font-weight: bold; font-size: 16px; margin-bottom: 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }} .movie-meta {{ font-size: 13px; color: #666; margin-bottom: 3px; }} .movie-rating {{ color: #e09015; font-weight: bold; }} .footer {{ text-align: center; margin-top: 40px; color: #999; font-size: 14px; }} @media (max-width: 768px) {{ .movie-card {{ width: 45%; }} }} @media (max-width: 480px) {{ .movie-card {{ width: 100%; }} }} </style> </head> <body> <h1>🎬 豆瓣热映电影</h1> <div class="movie-container"> {movies_html} </div> <div class="footer"> 数据来源: 豆瓣电影 | 更新时间: {update_time} </div> </body> </html> """ from datetime import datetime update_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') movies_html = "" for movie in movies: movie_html = f""" <div class="movie-card"> <img class="movie-poster" src="{movie.get('poster', '')}" alt="{movie.get('title', '')}"> <div class="movie-info"> <div class="movie-title">{movie.get('title', '')}</div> <div class="movie-meta">导演: {movie.get('director', '未知')}</div> <div class="movie-meta">主演: {movie.get('actors', '未知')}</div> <div class="movie-meta">地区: {movie.get('region', '未知')}</div> <div class="movie-meta">片长: {movie.get('duration', '未知')}</div> <div class="movie-rating">评分: {movie.get('score', movie.get('star_rating', '暂无评分'))}</div> </div> </div> """ movies_html += movie_html full_html = html_template.format(movies_html=movies_html, update_time=update_time) return full_html
def save_html(html_content, filename="docs/douban_movies.html"): os.makedirs("docs", exist_ok=True) with open(filename, "w", encoding="utf-8") as f: f.write(html_content)
if __name__ == "__main__": print("正在获取豆瓣热映电影信息...") movies = get_douban_hot_movies() if movies: print(f"获取到 {len(movies)} 部热映电影") html_content = generate_html(movies) save_html(html_content) else: print("未能获取热映电影信息")
|