From 69020fce24639b61485e0475d50e7388175aacd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=96=E6=AC=A3=E8=8C=B9?= <2509165049@student.example.com> Date: Tue, 31 Mar 2026 11:14:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- import requests.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 import requests.rb diff --git a/import requests.rb b/import requests.rb new file mode 100644 index 0000000..d49471a --- /dev/null +++ b/import requests.rb @@ -0,0 +1,44 @@ +import requests +import re +import json + +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'([^<&]+)' +rating_pattern = r']*>(\d+\.?\d*)' +quote_pattern = r'([^<]+)' + +titles = re.findall(title_pattern, html) +ratings = re.findall(rating_pattern, html) +quotes = re.findall(quote_pattern, html) + +movies = [] +title_index = 0 +for i in range(10): + # 跳过英文名(带/的) + while title_index < len(titles) and titles[title_index].startswith('/'): + title_index += 1 + + movie = { + 'rank': i + 1, + 'title': titles[title_index] if title_index < len(titles) else '', + 'en_title': '', + 'rating': ratings[i] if i < len(ratings) else '', + 'quote': quotes[i] if i < len(quotes) else '' + } + # 检查下一个是不是英文名 + if title_index + 1 < len(titles) and titles[title_index + 1].startswith('/'): + movie['en_title'] = titles[title_index + 1].replace('/ ', '') + + movies.append(movie) + title_index += 1 + +with open('movies.json', 'w', encoding='utf-8') as f: + json.dump(movies, f, ensure_ascii=False, indent=2) + +print('已保存到 movies.json')