爬取豆瓣电影top250
时间: 2023-10-24 13:06:04 浏览: 133
好的,你可以按照以下步骤来爬取豆瓣电影Top250:
1. 导入需要的库
```python
import requests
from bs4 import BeautifulSoup
```
2. 构造请求头和url链接
```python
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'}
```
3. 发送请求和解析HTML页面
```python
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
```
4. 提取电影信息
```python
movie_list = soup.find('ol', class_='grid_view ').find_all('li')
for movie in movie_list:
title = movie.find('span', class_='title').text
rating = movie.find('span', class_='rating_num').text
quote = movie.find('span', class_='inq').text
print('电影名称:', title)
print('评分:', rating)
print('简介:', quote)
print('-----------------')
```
这样就可以爬取豆瓣电影Top250,并输出每部电影的名称、评分和简介了。
阅读全文