用Python爬取豆瓣电影top
时间: 2023-11-05 11:22:40 浏览: 88
好的,这个问题很简单,你可以使用Python中的requests和BeautifulSoup库来爬取豆瓣电影top。以下是代码示例:
```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'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
movie_list = []
for movie in soup.find_all('div', class_='info'):
title = movie.find('span', class_='title').text
rating = movie.find('span', class_='rating_num').text
quote = movie.find('span', class_='inq').text
movie_list.append({'title': title, 'rating': rating, 'quote': quote})
for movie in movie_list:
print(movie['title'], movie['rating'], movie['quote'])
```
这个代码可以爬取豆瓣电影top250的电影名称、评分和简介,并将结果输出到控制台。注意,为了避免被反爬虫机制禁止访问,我们还设置了一个浏览器标识(User-Agent)。
阅读全文