download.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2025/11/22 13:07:40
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : pip install PySocks
  8. '''
  9. import requests
  10. import os
  11. def download():
  12. with open('image-url-1.md', 'r', encoding='utf-8') as f:
  13. lines = f.readlines()
  14. if not os.path.exists('downloads'):
  15. os.makedirs('downloads')
  16. sess = requests.Session()
  17. # sess.headers.update({
  18. # 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
  19. # 'accept-language': 'en-US,en;q=0.9',
  20. # 'cache-control': 'max-age=0',
  21. # 'if-modified-since': 'Fri, 04 Oct 2024 08:39:09 GMT',
  22. # 'if-none-match': '"9d88c0e13816db1:0"',
  23. # 'dnt': '1',
  24. # 'priority': 'u=0, i',
  25. # 'sec-ch-ua': '"Chromium";v="142", "Microsoft Edge";v="142", "Not_A Brand";v="99"',
  26. # 'sec-ch-ua-mobile': '?0',
  27. # 'sec-ch-ua-platform': '"Windows"',
  28. # 'sec-fetch-dest': 'document',
  29. # 'sec-fetch-mode': 'navigate',
  30. # 'sec-fetch-site': 'none',
  31. # 'sec-fetch-user': '?1',
  32. # 'upgrade-insecure-requests': '1',
  33. # 'user-agent': 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0'
  34. # })
  35. # set proxy: tcp://localhost:12180
  36. sess.proxies.update({
  37. 'http': 'socks5h://localhost:12180',
  38. 'https': 'socks5h://localhost:12180'
  39. })
  40. for line in lines:
  41. url = line.strip()
  42. if url:
  43. try:
  44. response = sess.get(url)
  45. response.raise_for_status()
  46. filename = os.path.join('downloads', url.split('/')[-1])
  47. with open(filename, 'wb') as img_file:
  48. img_file.write(response.content)
  49. print(f'Downloaded: {url} -> {filename}')
  50. except Exception as e:
  51. print(f'Failed to download {url}: {e}')
  52. if __name__=='__main__':
  53. download()