python爬取豆瓣中top250中的电影名称,导演和演员,评分,排名
时间: 2024-03-11 07:43:52 浏览: 264
好的,我可以帮你解决这个问题。你可以使用Python中的requests和BeautifulSoup库来爬取豆瓣电影的信息。以下是代码示例:
```python
import requests
from bs4 import BeautifulSoup
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'}
movie_info = []
for i in range(0, 250, 25):
params = {'start': str(i), 'filter': ''}
html = requests.get(url, params=params, headers=headers).text
soup = BeautifulSoup(html, 'html.parser')
movies = soup.find_all('div', class_='info')
for movie in movies:
title = movie.find('span', class_='title').text
director = movie.find('div', class_='bd').p.text.strip().split('\xa0')[0][4:]
actor = movie.find('div', class_='bd').p.text.strip().split('\xa0')[1][3:]
rating = movie.find('span', class_='rating_num').text
rank = movie.find('em', class_='').text
movie_info.append({'rank': rank, 'title': title, 'director': director, 'actor': actor, 'rating': rating})
# 打印电影信息
for info in movie_info:
print('排名:', info['rank'])
print('电影名称:', info['title'])
print('导演:', info['director'])
print('演员:', info['actor'])
print('评分:', info['rating'])
print('------------------------------------')
```
这段代码会爬取豆瓣电影的Top250,获取电影名称、导演、演员、评分和排名,并将其存储在一个列表中。最后,它会打印出每个电影的信息。注意:如果需要使用该代码,请遵守豆瓣网站的爬虫规则。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)