23 lines
686 B
Python
23 lines
686 B
Python
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")
|
|
print("前十部电脑名称已保存到movies.txt")
|
|
|