https://www.douban.com/doulist/3936288/只要标题和评分 250部电影的,Python爬虫
时间: 2024-10-13 15:04:43 浏览: 32
豆瓣电影Top 250列表是一个非常受欢迎的排行榜单,如果你想用Python编写一个简单的爬虫获取这些电影的信息,比如标题和评分,你可以使用第三方库如`requests`、`BeautifulSoup`或者`selenium`来抓取网页内容。以下是一个简化的示例,使用了`beautifulsoup4`库:
```python
import requests
from bs4 import BeautifulSoup
def get_top_250_movies():
url = 'https://movie.douban.com/top250'
headers = {'User-Agent': 'Mozilla/5.0'} # 设置请求头模拟浏览器访问
response = requests.get(url, headers=headers)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'lxml') # 解析HTML
movie_list = []
for item in soup.find_all('div', class_='hd'): # 找到每个电影条目
title = item.find('span', itemprop='name').text
rating = float(item.find('span', class_='rating_num')['title']) # 获取评分
movie_list.append({'title': title, 'rating': rating})
return movie_list
else:
print("Failed to fetch the data.")
movies_data = get_top_250_movies()
for movie in movies_data:
print(f"标题: {movie['title']}, 评分: {movie['rating']} 分")
阅读全文