爬虫爬取豆瓣top250的:序号 电影名 详情页的链接 导演 演员 评论人数 评分 语言 上映日期 上映地区 电影类型 ,并保存为csv文件
时间: 2024-06-10 19:05:03 浏览: 15
好的,我可以为您提供代码来完成这个任务。首先您需要安装爬虫库beautifulsoup4和requests,您可以使用以下命令进行安装:
```python
pip install beautifulsoup4 requests
```
接下来是代码实现:
```python
import requests
from bs4 import BeautifulSoup
import csv
# 请求头部信息,用于模拟浏览器访问
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'}
# 爬取页面的URL地址
url = 'https://movie.douban.com/top250'
# 用于存储爬取的电影信息的列表
movie_list = []
# 循环爬取每个页面
for i in range(10):
# 拼接每个页面的URL地址
page_url = url + f'?start={i*25}&filter='
# 使用requests库发起GET请求
response = requests.get(page_url, headers=headers)
# 使用BeautifulSoup解析HTML页面数据
soup = BeautifulSoup(response.text, 'html.parser')
# 获取电影节点列表
movie_nodes = soup.select('ol.grid_view > li')
# 遍历电影节点列表
for movie_node in movie_nodes:
# 将节点数据提取出来
movie_rank = movie_node.select_one('.pic em').get_text().strip() # 电影排名
movie_name = movie_node.select_one('.title').get_text().strip() # 电影名称
movie_link = movie_node.select_one('.hd a')['href'].strip() # 电影详情页链接
movie_directors = [director.get_text().strip() for director in movie_node.select('.bd p:nth-of-type(1)')[0].select('a')] # 电影导演
movie_stars = [star.get_text().strip() for star in movie_node.select('.bd p:nth-of-type(1)')[0].select('a')[1:]] # 电影演员
movie_comment_count = movie_node.select('.bd p:nth-of-type(2)')[0].get_text().strip().split(' ')[0] # 电影评论人数
movie_rating = movie_node.select_one('.rating_num').get_text().strip() # 电影评分
movie_language = movie_node.select('.bd p:nth-of-type(3)')[0].get_text().strip().split('/')[0].split(':')[1].strip() # 电影语言
movie_release_date = movie_node.select('.bd p:nth-of-type(3)')[0].get_text().strip().split('/')[2].strip() # 电影上映日期
movie_region = movie_node.select('.bd p:nth-of-type(3)')[0].get_text().strip().split('/')[1].strip() # 电影上映地区
movie_type = [movie_type.strip() for movie_type in movie_node.select('.bd p:nth-of-type(1)')[0].select('.genre')] # 电影类型
# 将电影信息保存到列表中
movie_list.append({
'排名': movie_rank,
'电影名称': movie_name,
'详情页链接': movie_link,
'导演': '/'.join(movie_directors),
'演员': '/'.join(movie_stars),
'评论人数': movie_comment_count,
'评分': movie_rating,
'语言': movie_language,
'上映日期': movie_release_date,
'上映地区': movie_region,
'电影类型': '/'.join(movie_type),
})
# 将电影信息保存为csv文件
with open('douban_top250.csv', 'w', newline='', encoding='utf-8-sig') as f:
# 声明csv文件写入器
writer = csv.DictWriter(f, fieldnames=['排名', '电影名称', '详情页链接', '导演', '演员', '评论人数', '评分', '语言', '上映日期', '上映地区', '电影类型'])
# 写入表头
writer.writeheader()
# 写入每一行电影信息
for movie in movie_list:
writer.writerow(movie)
print('电影信息已保存到douban_top250.csv文件中!')
```
以上是实现代码,使用时只需将代码放置到.py文件中运行即可。代码已经实现了爬取豆瓣电影TOP250并将电影信息保存为csv文件的功能。如有任何问题可以随时向我提出。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)