18 lines
676 B
Python
18 lines
676 B
Python
import requests
|
|
import re
|
|
url = 'https://movie.douban.com/top250'
|
|
headers = {
|
|
'User-Agent':'Mozilla/5.0(Windows NT 6.1: Win64; x64)AppleWebkit/537.36(KHTML,like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
|
}
|
|
|
|
response = requests.get(url,headers=headers)
|
|
response.encoding = 'utf-8'
|
|
html = response.text
|
|
pattern = r'<span class"title">(.*?)</span>'
|
|
titles = re.findall(pattern,html)
|
|
chinese_titles = [titles for title in titles if "/" not in title ]
|
|
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}.{titles}\n")
|
|
print("movies.txt已保存") |