python爬取豆瓣TOP250生成excel
时间: 2023-11-24 13:48:27 浏览: 87
要使用Python爬取豆瓣TOP250并生成Excel文件,可以按照以下步骤进行操作:
1. 使用requests库获取网页源代码,使用BeautifulSoup库解析网页内容,获取电影名称、评分、导演等信息。
2. 使用openpyxl库创建Excel文件,并在其中创建一个工作表。
3. 将获取到的电影信息写入Excel文件中。
4. 保存Excel文件。
下面是一个简单的代码示例:
```
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
# 获取网页源代码
url = 'https://movie.douban.com/top250'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 创建Excel文件
wb = Workbook()
ws = wb.active
ws.append(['电影名称', '评分', '导演'])
# 获取电影信息并写入Excel文件
for movie in soup.find_all('div', class_='info'):
name = movie.find('span', class_='title').text
rating = movie.find('span', class_='rating_num').text
director = movie.find('p').text.split('\n')[1].strip()
ws.append([name, rating, director])
# 保存Excel文件
wb.save('top250.xlsx')
```
阅读全文
相关推荐


















