Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

图片下载超时报错 #22

Open
Samoyeder opened this issue Oct 16, 2024 · 0 comments
Open

图片下载超时报错 #22

Samoyeder opened this issue Oct 16, 2024 · 0 comments

Comments

@Samoyeder
Copy link

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

在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
        
    # ... (后面的代码保持不变)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant