上传文件至 /
This commit is contained in:
32
1.py
Normal file
32
1.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import requests
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
import csv
|
||||||
|
import time
|
||||||
|
|
||||||
|
# 1. 发送请求
|
||||||
|
url = 'https://movie.douban.com/top250'
|
||||||
|
headers = {'User-Agent': 'Mozilla/5.0...'}
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
|
||||||
|
# 2. 解析数据
|
||||||
|
soup = BeautifulSoup(response.text, 'lxml')
|
||||||
|
movies = []
|
||||||
|
|
||||||
|
for item in soup.select('.item'):
|
||||||
|
title = item.select_one('.title').get_text()
|
||||||
|
rating = item.select_one('.rating_num').get_text()
|
||||||
|
quote = item.select_one('.inq').get_text() if item.select_one('.inq') else ''
|
||||||
|
|
||||||
|
movies.append({
|
||||||
|
'title': title.strip(),
|
||||||
|
'rating': rating,
|
||||||
|
'quote': quote
|
||||||
|
})
|
||||||
|
|
||||||
|
# 3. 保存为CSV
|
||||||
|
with open('movies.csv', 'w', newline='', encoding='utf-8') as f:
|
||||||
|
writer = csv.DictWriter(f, fieldnames=['title', 'rating', 'quote'])
|
||||||
|
writer.writeheader()
|
||||||
|
writer.writerows(movies)
|
||||||
|
|
||||||
|
print(f'已保存 {len(movies)} 部电影到 movies.csv')
|
||||||
BIN
movies.csv
Normal file
BIN
movies.csv
Normal file
Binary file not shown.
|
Reference in New Issue
Block a user