Python爬取豆瓣电影Top250数据

任务

爬取豆瓣电影top250中的影片名称、影片海报、年份、地区、类型、评分、评价人数、总体评价,并输出到douban_top250.xlsx文件中

环境

Python 3.8

requests

bs4

openpyxl

源码

python 复制代码
# 创建一个新的Excel工作簿
workbook = openpyxl.Workbook()
# 获取默认的工作表
sheet = workbook.active
# 写入数据
sheet['A1'] = '序号'
sheet['B1'] = '电影名'
sheet['C1'] = '海报'
sheet['D1'] = '年份'
sheet['E1'] = '地区'
sheet['F1'] = '类型'
sheet['G1'] = '评分'
sheet['H1'] = '评价人数'
sheet['I1'] = '总体评价'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
}
index = 1
for start_page in range(0, 250, 25):
    response = requests.get(f"https://movie.douban.com/top250?start={start_page}", headers=headers)
    html = response.text
    # html.parser表示使用html进行解析
    soup = BeautifulSoup(html, "html.parser")
    items = soup.find_all("div", attrs={"class": "item"})
    for item in items:
        # 海报
        post = item.find("img").get('src')
        # 名称
        name = item.find('span', class_="title").text
        # 年份
        infos = item.find('p', class_='').text.split("\n")[2].split("/")
        year = infos[0].strip()
        location = infos[1].strip()
        category = infos[2].strip()
        rate = item.find('span', class_='rating_num').text
        stars = item.find('div', class_='star')
        rate_people = stars.contents[7].text[:-3]
        review = ""
        if item.find('span', class_='inq') is not None:
            review = item.find('span', class_='inq').text
        sheet.append([index, name, post, year, location, category, rate, rate_people, review])
        index = index + 1
# 保存工作簿
workbook.save('./files/douban_top250.xlsx')

结果

相关推荐
青青草原羊村懒大王15 小时前
python基础知识三
开发语言·python
傻啦嘿哟15 小时前
Python高效实现Word转HTML:从基础到进阶的全流程方案
人工智能·python·tensorflow
wu_jing_sheng016 小时前
深度学习入门:揭开神经网络的神秘面纱(附PyTorch实战)
python
Ace_317508877616 小时前
淘宝店铺全量商品接口实战:分类穿透采集与增量同步的技术方案
大数据·数据库·python
LeonDL16816 小时前
基于YOLO11深度学习的电动车头盔检测系统【Python源码+Pyqt5界面+数据集+安装使用教程+训练代码】【附下载链接】
人工智能·python·深度学习·pyqt5·yolo数据集·电动车头盔检测系统·yolo11深度学习
wxin_VXbishe18 小时前
springboot在线课堂教学辅助系统-计算机毕业设计源码07741
java·c++·spring boot·python·spring·django·php
程序员三藏18 小时前
使用Docker和Selenium构建自动化测试环境
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
CaracalTiger19 小时前
本地部署 Stable Diffusion3.5!cpolar让远程访问很简单!
java·linux·运维·开发语言·python·微信·stable diffusion
api_1800790546019 小时前
【技术教程】Python/Node.js 调用拼多多商品详情 API 示例详解
大数据·开发语言·python·数据挖掘·node.js
0小豆020 小时前
智能字幕校准系统实战(二):6级匹配算法从精确到模糊的全链路解析
python·nlp·算法设计·spacy·ai算法·时间序列对齐