爬取豆瓣电影top250的所有代码
时间: 2023-08-30 10:12:36 浏览: 47
好的,以下是使用Python爬取豆瓣电影Top250的示例代码:
```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_list = []
for i in range(10):
req = requests.get(url, headers=headers, params={'start': i * 25})
soup = BeautifulSoup(req.text, 'html.parser')
movies = soup.select('.grid_view li')
for movie in movies:
title = movie.select('.title')[0].get_text()
link = movie.select('a')[0]['href']
star = movie.select('.rating_num')[0].get_text()
quote = movie.select('.inq')[0].get_text() if movie.select('.inq') else ''
movie_list.append({'title': title, 'link': link, 'star': star, 'quote': quote})
for movie in movie_list:
print(movie['title'], movie['link'], movie['star'], movie['quote'])
```
这个代码可以爬取豆瓣电影Top250的电影名称、电影链接、评分以及简介。你可以自由修改代码以适应你的需求。注意:请勿滥用爬虫,遵守网站的规则和法律法规。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)