【分享】小红书采集图片下载到本地

前面我们把图片链接都存到了excel里,现在想要把图片都下载到本地文件夹

下面是实现的python代码

图片链接存在第三列单元格,并且是多个图片\n分割的,现在把他们全部下载到本地

import pandas as pd
import requests

df = pd.read_excel("小红书.xlsx", header=None)
# 循环遍历每个单元格
for index, row in df.iterrows():
    num = index + 1
    if pd.isna(row[2]):
        continue

    imgs = row[2].split("\n")
    j = 1
    for img in imgs:
        fileName = f"imgs/{num}_{j}.jpg"
        print(fileName)

        # 图像的URL
        image_url = img

        # 发送HTTP请求获取图像
        response = requests.get(image_url)

        if response.status_code == 200:
            # 获取图像的二进制数据
            image_data = response.content

            # 保存图像到本地文件
            with open(fileName, 'wb') as f:
                f.write(image_data)
            print("图像已成功下载到本地。")
        else:
            print("无法下载图像。")
        j += 1