19 lines
711 B
Python
19 lines
711 B
Python
import requests
|
||
import re
|
||
|
||
url = "https://movie.douban.com/top250"
|
||
headers = {
|
||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||
}
|
||
response = requests.get(url, headers=headers)
|
||
html = response.text
|
||
|
||
pattern = re.compile(r'<span class="title">(.*?)</span>', re.S)
|
||
movie_names = pattern.findall(html)
|
||
|
||
chinese_names = [name for name in movie_names if not name.startswith('/')]
|
||
top10_names = chinese_names[:10]
|
||
with open("movies.txt", "w", encoding="utf-8") as f:
|
||
for name in top10_names:
|
||
f.write(name + "\n")
|
||
print("练习1完成,已保存前10部电影名称到movies.txt") |