mysql_image_down.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import requests
  5. import time
  6. from urllib.parse import urlparse
  7. # 要读取的txt文件路径
  8. txt_file = 'unique_urls1.txt'
  9. # 图片保存目录
  10. base_image_dir = 'completeimage2'
  11. # 创建保存图片的基础目录
  12. os.makedirs(base_image_dir, exist_ok=True)
  13. # 创建下载成功和下载失败的文件
  14. complete_file = 'downcomplete2.txt'
  15. error_file = 'downerror2.txt'
  16. # 读取txt文件中的所有行
  17. with open(txt_file, 'r') as file:
  18. lines = file.readlines()
  19. # 遍历每一行图片地址并下载
  20. for i, line in enumerate(lines, 1):
  21. url = line.strip()
  22. # 解析URL
  23. parsed_url = urlparse(url)
  24. try:
  25. # 下载图片
  26. response = requests.get(url, timeout=5)
  27. if response.status_code == 200:
  28. # 判断是否需要创建文件夹
  29. if parsed_url.path.split('/')[-1] != "":
  30. # 生成文件夹路径
  31. folder_path = os.path.join(
  32. base_image_dir, parsed_url.netloc, *parsed_url.path.strip("/").split("/")[:-1])
  33. # 创建文件夹
  34. os.makedirs(folder_path, exist_ok=True)
  35. # 提取文件名
  36. filename = url.split('/')[-1]
  37. # 保存图片
  38. with open(os.path.join(folder_path, filename), 'wb') as file:
  39. file.write(response.content)
  40. else:
  41. # 保存图片在基础目录下
  42. filename = url.split('/')[-1]
  43. with open(os.path.join(base_image_dir, filename), 'wb') as file:
  44. file.write(response.content)
  45. print(f"图片 {i}: {url} 下载完成。")
  46. # 写入下载成功的地址
  47. with open(complete_file, 'a') as file:
  48. file.write(url + '\n')
  49. else:
  50. print(f"图片 {i}: {url} 下载失败。")
  51. # 写入下载失败的地址
  52. with open(error_file, 'a') as file:
  53. file.write(url + '\n')
  54. except requests.exceptions.Timeout:
  55. print(f"图片 {i}: {url} 下载超时。")
  56. # 写入下载失败的地址
  57. with open(error_file, 'a') as file:
  58. file.write(url + '\n')
  59. except requests.exceptions.RequestException as e:
  60. print(f"图片 {i}: {url} 下载发生错误:{e}")
  61. # 写入下载失败的地址
  62. with open(error_file, 'a') as file:
  63. file.write(url + '\n')
  64. # 添加间隔
  65. time.sleep(1)