上传文件至 /
This commit is contained in:
33
1.py
Normal file
33
1.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import requests
|
||||
import re
|
||||
|
||||
headers ={
|
||||
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chroe/120.0.0.0 Safari/537.36",
|
||||
}
|
||||
url = 'https://movie.douban.com/top250'
|
||||
|
||||
|
||||
|
||||
response = requests.get(url,header=headers)
|
||||
html = response.text
|
||||
|
||||
pattern = r'<span class> = "title">([^<&]+)</span>'
|
||||
titles = pattern.findall(html , pattern)
|
||||
chinese_titles = [t for t in titles if not t.startswith('/')]
|
||||
top10_titles = chinese_titles[:10]
|
||||
with open ("movies.txt","w",encoding="utf-8") as f:
|
||||
for idx,titles in enumerate(top10_titles,1):
|
||||
f.write(f"{idx}. {title}\n")
|
||||
rating_pattern = r'<span class="rating_num"[^>]*>(\d+\.\d)</span>'
|
||||
ratings = re.findall(rating_pattern, html)
|
||||
|
||||
quote_pattern = r'<span class="inq">([^<]+)</span>'
|
||||
quotes = re.findall(quote_pattern, html)
|
||||
|
||||
print(f'{"排名":<4} {"电影名":<20} {"评分":<6} {"引言"}')
|
||||
print('-' * 70)
|
||||
for i in range(min(10, len(chinese_titles))):
|
||||
title = chinese_titles[i]
|
||||
rating = ratings[i] if i < len(ratings) else '无'
|
||||
quote = f'"{quotes[i]}"' if i < len(quotes) else '无'
|
||||
print(f'{i+1:<4} {title:<20} {rating:<6} {quote}')
|
||||
Reference in New Issue
Block a user