Skip to content

图片下载超时报错 #22

@Samoyeder

Description

@Samoyeder

如果笔记中有通过连接引入的图片,并且连接无法访问,那么下载程序会报错,后续图片下载会卡住

在export-image.py中修改

# ... (前面的代码保持不变)

for img_link in img_links:
    # ... (前面的代码保持不变)

    if download_image:
        download_folder = os.path.join(image_folder, years)
        if not os.path.exists(download_folder):
            os.mkdir(download_folder)

        img_url = urlunparse(parsed_url._replace(fragment=""))
        
        # 添加异常处理
        try:
            response = requests.get(img_url, timeout=10)  # 设置超时时间
            response.raise_for_status()  # 检查请求是否成功
            with open(os.path.join(download_folder, img_filename), "wb") as img_file:
                img_file.write(response.content)
                print(f"Downloaded: {img_filename}")
        except requests.exceptions.Timeout:
            print(f"[Warning]: Timeout while downloading {img_url}. Skipping.")
            continue
        except requests.exceptions.RequestException as e:
            print(f"[Error]: An error occurred while downloading {img_url}: {e}. Skipping.")
            continue
        
    # ... (后面的代码保持不变)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions