学习爬虫的第二天——分页爬取并存入表中

发布于:2025-03-27 ⋅ 阅读:(43) ⋅ 点赞:(0)

阅读提示:我现在还在尝试爬静态页面

一、分页爬取模式

以豆瓣Top250为例:

二、数据存取

Excel文件存储

  • pandas
  • openpyxl

2.1 openpyxl基本操作

from openpyxl import Workbook

# 创建新工作簿
wb = Workbook()

# 获取活动工作表(默认创建的第一个工作表)
ws = wb.active

# 创建新工作表
ws1 = wb.create_sheet("MySheet1")  # 默认插入到最后
ws2 = wb.create_sheet("MySheet2", 0)  # 插入到第一个位置

# 重命名工作表
ws.title = "New Title"
# 保存工作簿
wb.save("example.xlsx")

# 加载现有工作簿
from openpyxl import load_workbook
wb = load_workbook("example.xlsx")
# 写入数据
ws['A1'] = "Hello"  # 单个单元格
ws.cell(row=1, column=2, value="World")  # 行列指定

# 读取数据
print(ws['A1'].value)  # 输出: Hello
print(ws.cell(row=1, column=2).value)  # 输出: World

# 批量写入
for row in range(1, 11):
    for col in range(1, 5):
        ws.cell(row=row, column=col, value=row*col)

三、爬取代码

import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
import time

headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
    }
def GetFilm():
    base_url="https://movie.douban.com/top250"
    movies = []
    for start in range(0,250,25):
        url=f"{base_url}?start={start}"
        print(f"正在爬取: {url}")
        try:
            res=requests.get(url,headers=headers)
            soup=BeautifulSoup(res.text,'html.parser')
            items=soup.find_all('div',class_="item")
            for item in items:
                rank=item.find('em').text
                title=item.find('span',class_='title').text
                rating = item.find('span', class_='rating_num').text
                quote = item.find('p', class_='quote').text if item.find('p', class_='quote') else ""

                movies.append([
                    rank,title,rating,quote
                ])
            #添加延迟
            time.sleep(2)
        except Exception as e:
             print(f"爬取{url}时出错: {e}")
             continue

    return movies  # 确保返回列表
top_movies=GetFilm()
# 创建Excel工作簿
wb = Workbook()
ws = wb.active

# 添加表头
headers = ['排名', '电影名称', '评分', '短评']
ws.append(headers)

# 添加数据
for movie in top_movies:
    ws.append(movie)

# 保存Excel文件
excel_file = 'douban_top250_openpyxl.xlsx'
wb.save(excel_file)
print(f"数据已成功保存到 {excel_file}")


结果:


网站公告

今日签到

点亮在社区的每一天
去签到