豆瓣热映电影网页生成器

爱盼项目上显示了豆瓣热映和电视剧等信息,自己也想要一个这样的页面,不会写怎么办,呼叫deepseek生成一个。

生成的代码

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("未能获取热映电影信息")

脚本需要的Python库

  • requests
  • beautifulsoup4
    pip install requests beautifulsoup4

运行脚本

它会:

  • 从豆瓣电影获取当前热映电影信息;
  • 解析电影标题、评分、导演、演员、海报等信息;
  • 生成一个美观的HTML页面展示这些电影;
  • 将HTML保存为”douban_movies.html”文件。

添加豆瓣链接和评分星级

  • 在 generate_html() 函数前添加 generate_star_rating() 辅助函数:
    放在 generate_html() 函数定义之前,作为独立的辅助函数。

  • 修改 generate_html() 函数中的电影卡片HTML模板:
    替换原来的 movie_html 部分。
    修改部分完整代码:

    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
    def generate_star_rating(score):
    """生成星级评分"""
    try:
    score = float(score)
    full_stars = int(score // 2)
    half_star = 1 if score % 2 >= 1 else 0
    empty_stars = 5 - full_stars - half_star
    return '⭐' * full_stars + '½' * half_star + '☆' * empty_stars
    except:
    return ''

    def generate_html(movies):
    """
    生成展示热映电影的HTML页面
    """
    html_template = """
    <!DOCTYPE html>
    <html lang="zh-CN">
    <!-- 之前的HTML模板保持不变 -->
    </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">
    <a href="https://movie.douban.com/subject/{movie.get('id', '')}/" target="_blank">
    <img class="movie-poster" src="{movie.get('poster', '')}" alt="{movie.get('title', '')}">
    </a>
    <div class="movie-info">
    <div class="movie-title">
    <a href="https://movie.douban.com/subject/{movie.get('id', '')}/" target="_blank">
    {movie.get('title', '')}
    </a>
    </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', '暂无评分'))}
    {generate_star_rating(movie.get('score', 0))}
    </div>
    </div>
    </div>
    """
    movies_html += movie_html

    full_html = html_template.format(movies_html=movies_html, update_time=update_time)
    return full_html

注意事项

  • 豆瓣可能有反爬虫机制,图片会显示不了,可以尝试更换User-Agent或添加延迟
    添加Referrer Policy(简单方案)
    在HTML的中添加:
    <meta name="referrer" content="no-referrer">
  • 电影信息的HTML结构可能会变化,如果解析失败可能需要调整代码;
  • 生成的HTML页面是静态的,不会自动更新。

生成代码很容易,但也要一步一步改进才能得到想要的结果,比自己抠代码要简单多了。