编写代码,爬取https://movie.douban.com/top250网站,并按照电影名、导演、演员、时间、国家、电影类别、评分、评价人数以及评论打印出来
时间: 2023-12-28 21:18:42 浏览: 157
AMTK.ZIP_:www.amtk.com_AMTk:COm_amtk.com_amtk2.com_https://amtk2
5星 · 资源好评率100%
好的,我可以为您提供代码来爬取 https://movie.douban.com/top250 网站的信息。这里是 Python 的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 请求页面数据
url = 'https://movie.douban.com/top250'
r = requests.get(url)
# 解析页面内容
soup = BeautifulSoup(r.text, 'html.parser')
movies = soup.find_all('div', class_='info')
# 遍历每个电影信息并打印
for movie in movies:
# 获取电影名称
title = movie.find('span', class_='title').text
# 获取电影详情
detail_url = movie.find('a')['href']
detail_r = requests.get(detail_url)
detail_soup = BeautifulSoup(detail_r.text, 'html.parser')
# 获取导演、演员、时间、国家、电影类别等信息
directors = detail_soup.find_all('a', class_='', rel='v:directedBy')
director = '/'.join([d.text for d in directors])
actors = detail_soup.find_all('a', class_='', rel='v:starring')
actor = '/'.join([a.text for a in actors])
year = detail_soup.find('span', class_='year').text
countries = detail_soup.find_all('span', class_='pl', text='制片国家/地区:')
country = countries[0].next_sibling.strip() if countries else ''
categories = detail_soup.find_all('span', class_='pl', text='类型:')
category = categories[0].next_sibling.strip() if categories else ''
# 获取评分和评价人数
rating = detail_soup.find('span', class_='rating_num').text
ratings_count = detail_soup.find('span', class_='rating_people').text
# 获取评论
comments = detail_soup.find_all('div', class_='comment')
comments_text = '\n'.join([c.find('p').text for c in comments])
# 打印电影信息
print('电影名称:', title)
print('导演:', director)
print('演员:', actor)
print('时间:', year)
print('国家:', country)
print('电影类别:', category)
print('评分:', rating)
print('评价人数:', ratings_count)
print('评论:\n', comments_text)
print('----------------------------------------')
```
这段代码会输出每部电影的名称、导演、演员、时间、国家、电影类别、评分、评价人数和评论。请注意,这是一个示例代码,你可以根据需要自行修改和拓展。
阅读全文