This commit is contained in:
2509165029
2026-04-02 15:58:11 +08:00
parent 7f4778e1e1
commit c1ce7bda07

18
4.2.py Normal file
View File

@@ -0,0 +1,18 @@
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
url = 'https://movie.douban.com/top250'
response = requests.get(url, headers=headers)
html = response.text
title_pattern = r'<span class="title">([^<]+)</span>'
titles = re.findall(title_pattern, html)
chinese_titles = [t for t in titles if not t.startswith('/')]
print('电影名称前10部')
for i, title in enumerate(chinese_titles[:10], 1):
print(f'{i}. {title}')
rating_pattern = r'<span class="rating_num"[^>]*>(\d+\.\d)</span>'
ratings = re.findall(rating_pattern, html)