用爬虫爬取豆瓣top250电影名称和评分。爬250条数据,存在csv文件中
时间: 2024-02-29 16:57:19 浏览: 130
好的,以下是实现您需求的Python代码:
```python
import requests
from bs4 import BeautifulSoup
import csv
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/58.0.3029.110 Safari/537.3"
}
# 发送请求,获取响应
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
# 解析网页,获取电影名称和评分
movie_names = []
movie_scores = []
for item in soup.select(".item"):
movie_name = item.select_one(".title").get_text().strip()
movie_score = item.select_one(".rating_num").get_text().strip()
movie_names.append(movie_name)
movie_scores.append(movie_score)
# 将结果写入csv文件
with open("top250.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["电影名称", "评分"])
for i in range(250):
writer.writerow([movie_names[i], movie_scores[i]])
print("数据已成功写入csv文件!")
```
上述代码会爬取豆瓣电影top250的电影名称和评分,并将结果存储到`top250.csv`文件中。您只需要将以上代码复制到您的Python编辑器中,运行即可。
阅读全文