1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import os import requests
start_page = 1 end_page = 100 source_id = '1384001302181' dir_name = 'download save path' url_prefix = f'https://xxx.xxx.xxx.cn/{source_id}/files/mobile/' image_links = [url_prefix + str(i) + '.jpg' for i in range(start_page, end_page + 1)] save_dir = os.path.join(os.getcwd(), dir_name)
img_urls = [ 'https://images.unsplash.com/photo-1719937050814-72892488f741', 'https://images.unsplash.com/photo-1720048171527-208cb3e93192', 'https://images.unsplash.com/photo-1734409019142-1927557a77b9', 'https://images.unsplash.com/photo-1734108039189-f6c123288381', 'https://images.unsplash.com/photo-1734268486202-7e85b12a7fa2', ]
def download_image(url, save_path): content_type_map = { 'image/jpeg': 'jpg', 'image/png': 'png', 'image/gif': 'gif', 'image/webp': 'webp', 'image/svg+xml': 'svg', } try: response = requests.get(url, stream=True) response.raise_for_status() print(response.headers) save_path = save_path.endswith('.') and save_path or save_path + '.' + content_type_map[response.headers['Content-Type']]; with open(save_path, 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) print(f"Image downloaded: {save_path}") except requests.exceptions.RequestException as e: print(f"Error downloading image {url}: {e}")
if (not os.path.exists(save_dir)): os.makedirs(save_dir);
for index, url in enumerate(image_links): file_name = url[url.rfind('/') + 1:] save_path = os.path.join(os.getcwd(), dir_name, file_name) print(f'download: {url} to {dir_name}/{file_name}') download_image(url, save_path)
|