上传文件至 /
This commit is contained in:
BIN
260331-2509165004.py
Normal file
BIN
260331-2509165004.py
Normal file
Binary file not shown.
33
lxj.py
Normal file
33
lxj.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import requests
|
||||||
|
import re
|
||||||
|
|
||||||
|
# 1. 发送请求获取页面
|
||||||
|
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
|
||||||
|
|
||||||
|
# 2. 用正则表达式提取电影名称
|
||||||
|
# 电影名称在 <span class="title"> 中
|
||||||
|
pattern = r'<span class="title">([^<&]+)</span>'
|
||||||
|
titles = re.findall(pattern, html)
|
||||||
|
|
||||||
|
# 3. 过滤掉英文名(只保留中文名)
|
||||||
|
chinese_titles = [t for t in titles if not t.startswith('/')]
|
||||||
|
|
||||||
|
# 取前10个
|
||||||
|
top10 = chinese_titles[:10]
|
||||||
|
|
||||||
|
# 4. 保存到文本文件
|
||||||
|
with open('movies.txt', 'w', encoding='utf-8') as f:
|
||||||
|
for i, title in enumerate(top10, 1):
|
||||||
|
f.write(f'{i}. {title}\n')
|
||||||
|
|
||||||
|
print('已保存前10部电影到 movies.txt')
|
||||||
|
|
||||||
|
# 显示内容验证
|
||||||
|
with open('movies.txt', 'r', encoding='utf-8') as f:
|
||||||
|
print(f.read())
|
||||||
Reference in New Issue
Block a user