3.31
This commit is contained in:
24
爬虫3.31.py
Normal file
24
爬虫3.31.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
url = "https://movie.douban.com/top250?start=0&filter="
|
||||
|
||||
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}
|
||||
|
||||
requests = requests.get(url, headers=headers,timeout =10)
|
||||
requests.encoding = 'utf-8'
|
||||
|
||||
html = requests.text
|
||||
|
||||
pattern = r'<span>{{= sub_title}}</span>'
|
||||
titles = re.findall(pattern, html)
|
||||
|
||||
chinese_titles = [t for t in titles if not t.startswith('/')]
|
||||
|
||||
top10 = chinese_titles[:10]
|
||||
|
||||
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')
|
||||
Reference in New Issue
Block a user