Python爬取豆瓣电影Top250数据

任务

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

环境

Python 3.8
requests
bs4
openpyxl

源码

# 创建一个新的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')

结果

在这里插入图片描述

相关推荐

  1. python scrapy 豆瓣电影top250教程2

    2024-05-04 02:16:01       32 阅读
  2. 案例:豆瓣电影 Top250数据

    2024-05-04 02:16:01       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-04 02:16:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-04 02:16:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-04 02:16:01       82 阅读
  4. Python语言-面向对象

    2024-05-04 02:16:01       91 阅读

热门阅读

  1. 一步一步写线程之十一线程池应用内存池

    2024-05-04 02:16:01       33 阅读
  2. css实现瀑布流布局

    2024-05-04 02:16:01       31 阅读
  3. matlab绘制散点图

    2024-05-04 02:16:01       36 阅读
  4. L1-042 日期格式化

    2024-05-04 02:16:01       27 阅读
  5. 概述小样本学习的具体应用场景

    2024-05-04 02:16:01       32 阅读
  6. 寻找自己的25号底片

    2024-05-04 02:16:01       36 阅读
  7. 上海计算机学会2023年9月月赛C++丙组T1口令的分类

    2024-05-04 02:16:01       34 阅读
  8. 238. 除自身以外数组的乘积/41. 缺失的第一个正数

    2024-05-04 02:16:01       29 阅读
  9. 模型蒸馏吼吼吼

    2024-05-04 02:16:01       34 阅读