Python 爬虫:豆瓣电影Top250,包括电影导演、类型、年份、主演
时间: 2023-10-20 10:59:06 浏览: 66
好的,我可以帮你解决这个问题。你可以使用 Python 中的 requests 库和 BeautifulSoup 库来爬取豆瓣电影 Top250 页面的信息并进行解析。
首先,你需要使用 requests 库向豆瓣电影 Top250 页面发送请求并获取页面内容。例如:
```python
import requests
url = 'https://movie.douban.com/top250'
response = requests.get(url)
content = response.content
```
然后,你需要使用 BeautifulSoup 库来解析页面内容并提取电影信息。例如:
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(content, 'html.parser')
movie_list = soup.find_all('div', class_='hd')
for movie in movie_list:
# 获取电影名称和链接
title = movie.a.span.text.strip()
link = movie.a['href']
# 获取电影导演、类型、年份、主演
info = movie.next_sibling.next_sibling.find('div', class_='bd').p.text.strip()
director = info.split('\n')[0].split(':')[1].strip()
actors = info.split('\n')[1].split(':')[1].strip()
genres = info.split('\n')[2].split(':')[1].strip().split('/')
year = info.split('\n')[3].split(':')[1].strip()
print(title, link, director, actors, genres, year)
```
这样,你就可以获取豆瓣电影 Top250 页面的电影信息了。希望对你有帮助!
阅读全文
相关推荐















